Several catch branches : Exception Try Catch « Language Basics « C# / C Sharp






Several catch branches

Several catch branches
/*
Learning C# 
by Jesse Liberty

Publisher: O'Reilly 
ISBN: 0596003765
*/

 using System;

 namespace ExceptionHandling
 {
    public class TesterExceptionHandling4
    {

       public void Run()
       {
           try
           {
               double a = 5;
               double b = 0;
               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)
           {
               Console.WriteLine(
                   "DivideByZeroException caught!");
           }

           catch (System.ArithmeticException)
           {
               Console.WriteLine(
                   "ArithmeticException caught!");
           }

               // generic exception type last
           catch
           {
               Console.WriteLine(
                   "Unknown exception caught");
           }
       }

        // 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...");
            TesterExceptionHandling4 t = new TesterExceptionHandling4();
            t.Run();
            Console.WriteLine("Exit Main...");
        }


    }
 }

           
       








Related examples in the same category

1.Catch Error
2.illustrates an unhandled exceptionillustrates an unhandled exception
3.illustrates how to handle a specific exceptionillustrates how to handle a specific exception
4.illustrates multiple catch blocksillustrates multiple catch blocks
5.illustrates a nested try/catch blockillustrates a nested try/catch block
6.illustrates exception propagation with methodsillustrates exception propagation with methods
7.Catch Divide By Zero ExceptionCatch Divide By Zero Exception
8.Demonstrates stacking catch blocks to provide alternate code for more than one exception type
9.Catches an exception that was thrown in a component
10.Throw a format exception purposely to demonstrate catching a FormatException
11.Demonstrates using if statements to sort out an IOException
12.Demonstrate exception handlingDemonstrate exception handling
13.An exception can be generated by one method and caught by anotherAn exception can be generated by one 
   method and caught by another
14.Exception Type MismatchException Type Mismatch
15.Handle error gracefully and continueHandle error gracefully and continue
16.Use multiple catch statementsUse multiple catch statements
17.Use the 'catch all' catch statementUse the 'catch all' catch statement
18.Use a nested try blockUse a nested try block
19.Catch different exceptions
20.Passing Exceptions on to the Caller: Caller Confuse
21.Passing Exceptions on to the Caller: Caller Inform
22.Exception Handling:Trying and CatchingException Handling:Trying and Catching