简体   繁体   中英

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.

With my class I have a static method string Print(string path) that reads the inputs from a flat file. 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() .

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).

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. 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

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

You need to use a try catch block. See this reference .

You can do the cleanup tasks you need in the catch and finally blocks.

in your class you can use throw statement if something is wrong with your class. And then when you working with you class you should use try-catch-finally block

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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