views:

83

answers:

4

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;
     }
}
+2  A: 

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

Phil Brown
beat me to it, you can handle your cleanup with a finally block. If you are going to be reading in from files and thus could run into files being left open you should also check out the using statement.
pstrjds
A: 

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.

Unmesh Kondolikar
A: 

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

RyuuGan
Do I have to write like a custom exception?
Shawn
you could write your own custom exception it is not very hard, but you don't really need to. There are many excepion classes in .Net that will cover most situations you encounter. http://msdn.microsoft.com/en-us/library/ms229064.aspx
Kieran
A: 

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