简体   繁体   English

C#异常处理

[英]C# Exception Handling

I have class that predominately consists of gather string input and then subsequently outputting the data in a certain format. 我的类主要由收集字符串输入组成,然后以某种格式输出数据。 The result of the class is basically a ToString() override. 该类的结果基本上是一个ToString()重写。

With my class I have a static method string Print(string path) that reads the inputs from a flat file. 对于我的班级,我有一个静态方法string Print(string path) ,它从平面文件中读取输入。 It then parses these inputs and generates an instance of a class for each record in the flat file. 然后,它解析这些输入并为平面文件中的每个记录生成一个类的实例。 Then for each instance of the class I call the class.ToString() and append it to the stringbuilder that eventually gets returned within Print() . 然后,对于该类的每个实例,我调用class.ToString()并将其附加到最终在Print()返回的stringbuilder。

I ensure that each record has the necessary values and have appropriate range, if they do not I need to throw an exception. 我确保每条记录都具有必要的值并具有适当的范围,如果不需要,则需要引发异常。 I've never done exception handling before so I want to know if what I want to do is even possible. 我以前从未做过异常处理,所以我想知道我想做的事是否可能。

When an exception case gets thrown, I want to take whatever is in the stringbuilder add the closing tag cleanup stuff and then prepend the exception text to the stringbuilder and then return (Exception Error Text + stringbuilder.ToString() + FooterStuff). 当引发异常情况时,我想获取stringbuilder中的所有内容,添加结束标记清除内容,然后将异常文本放在stringbuilder之前,然后返回(异常错误文本+ stringbuilder.ToString()+ FooterStuff)。

Edit Code: 修改代码:

It might not actually be a good idea to throw an exception, I might just need to use a try{} catch {} to catch an exception and then append the exception.message to the beginning of the stringbuilder. 引发异常实际上并不是一个好主意,我可能只需要使用try {} catch {}来捕获异常,然后将exception.message附加到stringbuilder的开头。 I don't really know though, exceptions are fairly new to me. 虽然我真的不知道,但是异常对我来说还是很新的。

public class Record
{
    public string Name;
    public string Value;
    ...

    private Dictionary<string, LogFormat> = new LogFormat.Table();

    public static string Print()
    {
          Stringbuilder sb = new StringBuilder();
          var k = ReadLog();

          foreach (var x in k)
          {
               sb.Append(x.ToString());
          }

          return sb.ToString();
    }

    public override string ToString()
    {
        if (Table[Name].NeedsValue && (Value == String.Empty || Value == null))
        {
            throw new Exception();
        }

        return String.Format(Table[Name].Format, Attribute, Value);
    }
}

public class LogFormat
{
     public string Format;
     public bool NeedsValue = false;

     public Dictionary<string,LogFormat> Table()
     {
           Dictionary<string,LogFormat> dct = new Dictionary<string,LogFormat>();
           dct.Add("Address", new LogFormat(){Format = "Street:{0}\nCity:{1}"});
           ...
           return dct;
     }
}

I'm not totally sure what you want to achieve but it sounds like you want to use finally 我不确定您要实现什么,但是听起来好像您finally想使用

http://msdn.microsoft.com/en-us/library/zwc8s4fz(VS.71).aspx http://msdn.microsoft.com/en-us/library/zwc8s4fz(VS.71).aspx

You need to use a try catch block. 您需要使用try catch块。 See this reference . 请参阅此参考

You can do the cleanup tasks you need in the catch and finally blocks. 您可以在catch中执行所需的清理任务,最后执行阻止。

in your class you can use throw statement if something is wrong with your class. 在班级中,如果班级出了问题,可以使用throw语句 And then when you working with you class you should use try-catch-finally block 然后在上课时,您应该使用try-catch-finally块

You can try something like that: 你可以试试这样的东西:

private string YourMethod()
{
    var sb = new StringBuilder();

    try
    {
        // Do your stuff
    }
    catch (ASpecificException ex)
    {
        sb.Insert(0, ex.Message);
    }
    finally
    {
        sb.Append("YourFooter");
    }

    return sb.ToString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM