Multiple catch block in java

So far we have seen how to use a single catch block, now we will see how to use more than one catch blocks in a single try block.
we can have multiple catch blocks for a particular try block to handle many different kind of exceptions that may be generated while running the program i.e. you can use more than one catch clause in a single try block however every catch block can handle only one type of exception. this mechanism is necessary when the try block has statement that raise different type of exceptions.


Syntax to use multiple catch-block with single try-block.

We can use any number of catch-block with one try-block.
There is a few limitation :

  Rule : All catch blocks must be ordered from most specific(Child) to most general(Parent) i.e. catch for ArithmeticException must come before catch for Exception .

  Rule : At a time only one Exception is occured and at a time only one catch block is executed.

Syntax:


try
{
    //error prone statements 
}
catch(Exception_type1 t)
{
    //first handling block
}
catch(Exception_type2 t)
{
    //second handling block
}
catch(Exception_type3 t)
{
    //third handling block
}
catch(Exception_type4 t)
{
    //4rth handling block
}
.
.
.
.catch(Exception_typeN t)
{
    //nth hadling block
}

Example of Multiple-catch block

how to use multiple catch block .


//example of multiple catch-block
class Example {
    public static void main(String arr[]) { 
         System.out.println("Start..");
   
        try
        {
            int a[]={1,0,3};
            int c=a[0]/a[1];
            System.out.println("Division = "+c);
        }
        catch(ArithmeticException e)
        {
            System.out.println("Run time error has occurred.");
            System.out.println(e.getMessage());
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("Run time error has occurred.");
            System.out.println(e.getMessage());

        }
        catch(NumberFormatException e)
        {
            System.out.println("Run time error has occurred.");
            System.out.println(e.getMessage());

        }            
         System.out.println("End..");
    }        
}
Start..
Run time error has occurred.
/ by zero
End..

If we try to catch Parent type Exception before catching Child type Exception then we will get compile time error . for example ...


//example of multiple catch-block
class Example {
    public static void main(String arr[]) { 
         System.out.println("Start..");
   
        try
        {
            int a[]={1,0,3};
            int c=a[0]/a[5];
            System.out.println("Division = "+c);
        }
        catch(Exception e)
        {
            System.out.println("Run time error has occurred.");
            System.out.println(e.getMessage());

        }
        catch(ArithmeticException e)
        {
            System.out.println("Run time error has occurred.");
            System.out.println(e.getMessage());
        }
                          
         System.out.println("End..");
    }        
}
Compile-Time error
error: exception ArithmeticException has already been caught
catch(ArithmeticException e)

we can catch Child Type Exception first then Parent Type. for example


//example of multiple catch-block
class Example {
    public static void main(String arr[]) { 
         System.out.println("Start..");
   
        try
        {
            int a[]={1,0,3};
            int c=a[0]/a[1];
            System.out.println("Division = "+c);
        }
        //handling Child type exception
        catch(ArithmeticException e)
        {
            System.out.println("Run time error has occurred.");
            System.out.println(e.getMessage());
        }
        //handling Parent type exception 
        catch(Exception e)
        {
            System.out.println("Run time error has occurred.");
            System.out.println(e.getMessage());

        }              
         System.out.println("End..");
    }        
}
Start..
Run time error has occurred.
/ by zero
End..