Note that there are some explanatory texts on larger screens.

plurals
  1. POC# Exception Handling
    primarykey
    data
    text
    <p>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.</p> <p>With my class I have a static method <code>string Print(string path)</code> 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 <code>Print()</code>.</p> <p>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.</p> <p>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).</p> <p>Edit Code:</p> <p>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.</p> <pre><code>public class Record { public string Name; public string Value; ... private Dictionary&lt;string, LogFormat&gt; = 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 &amp;&amp; (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&lt;string,LogFormat&gt; Table() { Dictionary&lt;string,LogFormat&gt; dct = new Dictionary&lt;string,LogFormat&gt;(); dct.Add("Address", new LogFormat(){Format = "Street:{0}\nCity:{1}"}); ... return dct; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload