c# exceptions & debug
Look familiar?   Can't convert letters into 'ints'...
SYNTAX, RUN-TIME, LOGIC Unhandled Exception: System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at MyProgram.Main(String[] args)

 .. simple try .. catch

Any exception will be caught, but the error information is poor. int age; age = int.Parse( Console.ReadLine() ); Error! if letters input (see above)
int age; try { age = int.Parse( Console.ReadLine() ); Console.WriteLine("number parsed correctly..."); } catch { Console.WriteLine("input error"); // generic info - no abort }

 .. try .. catch with exception object

Any exception will be caught, and the error info is better. int age; try { age = int.Parse( Console.ReadLine() ); Console.WriteLine("number parsed correctly..."); } catch (Exception ex) { // specific exception info - no abort Console.WriteLine("\nException: " + ex.Message); Console.WriteLine("\nMethod: " + ex.TargetSite); Console.WriteLine("\nStack Trace: " + ex.StackTrace); }

 .. finally

Any exception will be caught, and the error info is better. Also, "cleanup code" can be executed whether there was an error or not... int age; try { age = int.Parse( Console.ReadLine() ); Console.WriteLine("number parsed correctly..."); } catch (Exception e) { Console.WriteLine(e.Message); // specific error info - no abort } finally { Console.WriteLine("turn out the lights on the way out!"); }

 .. TryParse

A BIG source of errors is trying to turn non-numeric data into numbers... TryParse can avoid a LOT of errors int.TryParse() double.TryParse() * return true/false whether it was succesful, but does NOT abort on bad input
int iNum; bool ok; // a variable that is true or false // loop until you get valid numeric input do { Console.Write( "Please enter an integer to square: " ); ok = int.TryParse( Console.ReadLine( ), out iNum ); } while( ok == false );
//================================================================== // get input from a textbox, check for validity and range // tries ONCE, then clears textBox and puts focus on textbox after error message public static bool GetTextBoxInput(System.Windows.Forms.TextBox textBox, out int num, int min, int max) { if( int.TryParse(textBox.Text, out num) && num<=max && num >=min ) return true; System.Windows.Forms.MessageBox.Show(textBox.Text + " is not a legal input for this operation."); textBox.Clear(); textBox.Focus(); return false; }
//================================================================== // prompt, get input from user and check for validity and range // loop until input is good public static bool GetConsoleInput(out double num, string prompt, double min, double max) { do { System.Console.Write(prompt + ": (range of values is " + min + " to " + max + ") "); } while ( !double.TryParse( System.Console.ReadLine(), out num ) || num < min || num > max ); return true; }
checked divide by zero debug validate data/outcomes debug.assert regression test