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