Try Catch block in Java

In this tutorial, we will learn about Try Catch Block in Java. Using Try Catch block is the best way to handle exceptions in Java. We keep all the code that might throw an exception in the try block. The try block is followed by the catch and finally blocks. In this catch and finally blocks, we write code to handle the exception.

try{    
//Here is code that may throw an exception    
}catch(Exception_Class ref){
}   

Before going further, here we write a code(with exception) without Try Catch block:

Example Program(without Try Catch Block)

public class Example{
  public static void main(String args[]){
    System.out.println("Ans of 10/2 ="+(10/2));
    System.out.println("Ans of 12/3 ="+(12/3));
    System.out.println("Ans of 5/0 ="+(5/0));
    System.out.println("Ans of 24/6 ="+(24/6));
    System.out.println("Ans of 8/2 ="+(8/2));
  }
}

Output:

Ans of 10/2 =5
Ans of 10/2 =4
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at Example.main(Example.java:5)}

As you can see in the above example program we got an ArithmeticException(Because we can’t divide a number by zero in Java) on the fifth line. Now in the next example, we will write the same program but with the Try-Catch block to handle the Arithmetic Exception we got in our last example.

Example Program(With Try-Catch):

public class Example{
  public static void main(String args[]){
    System.out.println("Ans of 10/2 ="+(10/2));
    System.out.println("Ans of 12/3 ="+(12/3));
    try{
    System.out.println("Ans of 5/0 ="+(5/0));
    }catch(ArithmeticException e){
      System.out.println(e.getMessage());
    }
    System.out.println("Ans of 24/6 ="+(24/6));
    System.out.println("Ans of 8/2 ="+(8/2));
  }
}

Output:

Ans of 10/2 =5
Ans of 12/3 =4
/ by zero
Ans of 24/6 =4
Ans of 8/2 =4

Here you can see how we handled ArithmeticException in our last example using try catch block. In the try block, we placed all the code which might throw an exception and we catch this exception using catch block.

Instead of adding particular exception class such as ArithmeticException or ArrayIndexOutOfBoundsException you can directly add parent Exception class.

Example Program:

public class Example{
  public static void main(String args[]){
    System.out.println("Ans of 10/2 ="+(10/2));
    System.out.println("Ans of 12/3 ="+(12/3));
    try{
    System.out.println("Ans of 5/0 ="+(5/0));
    }catch(Exception e){
      System.out.println(e.getMessage());
    }
    System.out.println("Ans of 24/6 ="+(24/6));
    System.out.println("Ans of 8/2 ="+(8/2));
  }
}

Output:

Ans of 10/2 =5
Ans of 12/3 =4
/ by zero
Ans of 24/6 =4
Ans of 8/2 =4

Multiple catch blocks:

In many situations, your code might throw multiple types of exception so to solve different kind of exception we can add multiple catch block with a try block. Check out the following example.

Example Program:

public class Example{
   public static void main(String args[]){
     try{
         int num[]=new int[5];
         num[4]=10/0;
         System.out.println("Done");
     }
     catch(ArithmeticException e){
        System.out.println("Can not divide by Zero");
     }
     catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Out of Array Index");
     }
   System.out.println("Outside try-catch block");
   System.out.println("Outside try-catch block");
  }
}

Output:

Can not divide by Zero
Outside try-catch block
Outside try-catch block

As in the above example, you can see we have added multiple catch blocks(ArithmeticException and ArrayIndexOutOfBoundsException) and in the above example, we are got handled ArithmeticException. Here is the same example again with little change to show you the concept of multiple catch blocks.

Example Program:

public class Example{
   public static void main(String args[]){
     try{
         int num[]=new int[5];
         num[6]=10/2;
         System.out.println("Done");
     }
     catch(ArithmeticException e){
        System.out.println("Can not divide by Zero");
     }
     catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Out of Array Index");
     }
   System.out.println("Outside try-catch block");
   System.out.println("Outside try-catch block");
  }
}

Output:

Out of Array Index
Outside try-catch block
Outside try-catch block

Please note, if you want to add multiple catch blocks with try block then always add parent Exception class in the last catch block.

Example Program:

public class Example{
   public static void main(String args[]){
     try{
         int num[];
         num[6]=10/2;
         System.out.println("Done");
     }
     catch(ArithmeticException e){
        System.out.println("Can not divide by Zero");
     }
     catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Out of Array Index");
     }
    catch(Exception e){
        System.out.println("Exception in Code");
     }
   System.out.println("Outside try-catch block");
   System.out.println("Outside try-catch block");
  }
}

Finally Block in Java:

We can add finally block at the end of all the catch blocks. Finally block is mainly used to hold cleanup code such as you can code to close a database connection. The important thing to note about finally block is the code inside the finally both runs in both situation whether exception occur or not. Please check out the following example.

public class Example{
   public static void main(String args[]){
     try{
         int num[]=new int[5];
         num[3]=10/2;
         System.out.println("Done");
     }
     catch(ArithmeticException e){
        System.out.println("Can not divide by Zero");
     }
     catch(ArrayIndexOutOfBoundsException e){
        System.out.println("Out of Array Index");
     }
     finally{
       System.out.println("This is finally block");
     }
   System.out.println("Outside try-catch block");
   System.out.println("Outside try-catch block");
  }
}

Output:

Done
This is finally block
Outside try-catch block
Outside try-catch block

 

Spread the love
Scroll to Top
×