using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSharpExceptionHandling { class Program { public void Run() { try { double a; double b; System.Console.WriteLine ("Please Enter A Number: "); a = double.Parse(Console.ReadLine()); System.Console.WriteLine ("Please Enter Another Number: "); b = double.Parse(Console.ReadLine()); Console.WriteLine("Dividing {0} by {1}...", a, b); //The method doDivide throws two tyoes of exceptions: //- DivideByZeroException: Thrown if the user enters 0 as the second number (Number can't be divided by 0) //- ArithmeticException: Thrown if the user enters 0 as the first number Console.WriteLine("{0} / {1} = {2}", a, b, doDivide(a, b)); } // Most derived exception type first catch (System.DivideByZeroException e) { Console.WriteLine( "\nDivideByZeroException! Msg: {0}", e.Message); } catch (System.ArithmeticException e) { Console.WriteLine( "\nArithmeticException! Msg: {0}", e.Message); } // Generic exception type last catch { Console.WriteLine( "Unknown exception caught"); } finally { //This block IS ALWAYS Executed Console.WriteLine("'finally' block is always executed. Close file here."); } } // Do the division if legal public double doDivide(double a, double b) { if (b == 0) throw new System.DivideByZeroException(); if (a == 0) throw new System.ArithmeticException(); return a / b; } static void Main() { Console.WriteLine("Enter Main..."); Program t = new Program(); t.Run(); Console.WriteLine("Exit Main..."); Console.ReadLine(); } } }