static void Main(string[] args) { Console.WriteLine("***** MyMath Console ******"); // Exception handler for MyMath1 try { Console.WriteLine("101 + 99 = {0}", MyMath1.Add((byte)101, (byte)99)); Console.WriteLine("101 + 201 = {0}", MyMath1.Add(101, 201)); } catch (Exception) { Console.WriteLine("Value is exceeded."); } // Exception handler for MyMath2 try { Console.WriteLine("101 + 99 = {0}", MyMath2.Add((byte)101, (byte)99)); Console.WriteLine("101 + 201 = {0}", MyMath2.Add(101, 201)); } catch (Exception) { Console.WriteLine("Value is exceeded."); } }
// Input: two arthmatic problems adding byte types // Process: add together and observe the results // in one method let sum overflow the type max value // in other method throw an exception // Output: display sums on screen using methods from each class static void Main(string[] args) { // Output from steps 1 - 5: // casting 101 and 99 to bytes to make sure // am sending bytes to method in MyMath1.cs Console.WriteLine("Output from MyMath1:"); Console.WriteLine("\t 101 + 99 = {0}", MyMath1.Add((byte)101, (byte)99)); // deliberately cause an overflow // bytes hold max value of 256, so if you add more than that it overflows and // disposes of the significant value and keeps the insignificant value Console.WriteLine("\t101 + 201 = {0}", MyMath1.Add(101, 201)); Console.WriteLine(); // Steps // because it overflows method in MyMath2.cs will check it & throw error Console.WriteLine("Output from MyMath2:"); try { Console.WriteLine("\t101 + 99 = {0}", MyMath2.Add((byte)101, (byte)99)); // deliberately cause an overflow Console.WriteLine("\t101 + 201 = {0}", MyMath2.Add(101, 201)); } // catch the overflow as an exception catch (OverflowException e) { Console.WriteLine("\t" + e.Message); } Console.WriteLine(); }
static void Main(string[] args) { Console.WriteLine("101 + 99 = {0}", MyMath1.Add((byte)101, (byte)99)); Console.WriteLine("101 + 201 = {0}", MyMath1.Add(101, 201)); Console.WriteLine(); Console.WriteLine("101 + 99 = {0}", MyMath2.Add((byte)101, (byte)99)); Console.WriteLine("101 + 201 = {0}", MyMath2.Add(101, 201)); }
static void Main(string[] args) { Console.WriteLine("101 + 99 = {0}", MyMath1.Add(101, 99)); Console.WriteLine("101 + 201 = {0}", MyMath1.Add(101, 201)); try { Console.WriteLine("101 + 99 = {0}", MyMath2.Add(101, 99)); Console.WriteLine("101 + 201 = {0}", MyMath2.Add(101, 201)); } catch (Exception) { throw; } }
static void Main(string[] args) { Console.WriteLine(" --- MyMath1ConsoleApp MyMath1 Method ---"); // add code to console app Main and test Add Method Console.WriteLine("55 + 2 = {0}", MyMath1.Add((byte)55, (byte)2)); // test for sum greated than byte at 256 Console.WriteLine("200 + 80 = {0}", MyMath1.Add(200, 80)); // try for checked try { Console.WriteLine("200 + 80 = {0}", MyMath2.Add((byte)200, (byte)80)); } catch (System.OverflowException) { Console.WriteLine("The total of these number is larger than 256."); } }
static void Main(string[] args) { Console.WriteLine(); Console.WriteLine("101 + 99 = {0}", MyMath1.Add((byte)101, (byte)99)); Console.WriteLine(); Console.WriteLine("101 + 201 = {0}", MyMath1.Add(101, 201)); try { Console.WriteLine(); Console.WriteLine("101 + 99 = {0}", MyMath2.Add((byte)101, (byte)99)); Console.WriteLine(); Console.WriteLine("101 + 201 = {0}", MyMath2.Add(101, 201)); } catch (OverflowException ex) { Console.WriteLine(ex.Message); Console.WriteLine(); } }
static void Main(string[] args) { // prints and calls add method // for test 1: within byte range Console.WriteLine("** Testing Add Method and Byte Range **\n"); Console.WriteLine("55 + 65 = {0}", MyMath1.Add((byte)55, (byte)65)); Console.WriteLine("156 + 200 = {0}", MyMath1.Add((byte)156, (byte)200)); Console.WriteLine("\n\n"); Console.WriteLine("** With checked keyword and try/catch block: **\n"); try { Console.WriteLine("55 + 65 = {0}", MyMath2.Add((byte)55, (byte)65)); Console.WriteLine("156 + 200 = {0}", MyMath2.Add((byte)156, (byte)200)); } catch (OverflowException e) { Console.WriteLine("Program stopped. Exception Caught:\n\t\t {0}", e.Message); } Console.WriteLine("\n\n"); }