using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ExceptionsC { class Program { } class Tester { 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); 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 { Console.WriteLine("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(string[] args) { Console.WriteLine("Enter Main..."); Tester t = new Tester(); t.Run(); Console.WriteLine("Exit Main..."); Console.ReadLine(); } } }