// The following demonstrates exception handling in cSharp Set all projects in Solution to a Default Namespace of "csExceptionHandling" // C# Console Application Project "csExceptionHandling" // Set Project Refs to csMath1 & csMath2 using System; namespace ExeceptionHandling { class ExeceptionHandlingClient { static void Main(string[] args) { string answer; Math1 oMath1 = new Math1(); Math2 oMath2 = new Math2(); try { answer = oMath1.ByteAdd(255,2,true).ToString(); // if X + Y > 256 AND 3rd argument is true then an error will be thrown // If oMath1 is changed to oMath2 the "Source" will indicate same. Console.Write("The answer was: " + answer); } catch (OverflowException e) { string s = e.Source; Console.Write("Oops... we seem to have an overflow from [" + s + "]" ); } catch (Exception e) { Console.Write("\nSource: " + e.Source ); Console.Write("\nMessage: " + e.Message); } finally { Console.Write("\nPress Enter Key to Exit"); Console.ReadLine(); } } } } ///////////////////////////////////////////////////////////////////////////////////////// // C# Class Library Project "csMath1" // using System; namespace ExeceptionHandling { public class Math1 { public string ByteAdd(byte X , byte Y , bool chk) { string sSum; int intSum; intSum = X + Y; byte bSum; if (chk==true) { checked { bSum = (byte)intSum; } } else { unchecked { bSum = (byte)intSum; } } sSum = bSum.ToString(); return sSum; } } } ///////////////////////////////////////////////////////////////////////////////////////// // 2nd C# Class Library Project same as the above except change "csMath1" to "csMath2" // using System; namespace ExeceptionHandling { public class Math2 ... /////////////////////////////////////////////////////////////////////////////////////////