Try with Multiple Catch Blocks in Java
In some cases, codes inside the try block can throw multiple types of exceptions. Since, one catch block can only catch single type of exception, we have to create multiple catch blocks to catch different exceptions. In java, single try block can have more than one catch blocks. For example,
try { //code } catch(Exception e) //Incorrect way, not recommended { //handling code }
The correct way of handling multiple exceptions from the try block
try { //code } catch(ArithmeticException ae) { //handling code } catch(FileNotFoundException fe) { //handling code } catch(NullPointerException ne) { //handling code } catch(Exception e) //parent class should always be at last { //handling code }
Note- Order in which exceptions handling takes place is very important. This order should be from child to parent rather than parent to child. If in any case the order is parent to child, then the compiler will give compile time error as “Exception xxxx has already been caught”. For example,
try { //code } catch(Exception e) { //handling code } catch(ArithmeticException ae) { //handling code }
This is incorrect way, as ArithmeticException will already be caught by previous catch block and second catch block will never even execute. Correct way is as follows
try { //code } catch(ArithmeticException ae) { //handling code } catch(Exception e) { //handling code }
Finally Block
One should never define the clean up code of the program in the try block. There are possibilities that execution of these lines will never take place. Also one should not define the clean up code in the catch block either, as there are possibilities that this block will not execute if no exception is thrown. So the requirement is to put the clean up code in the program another block. This will execute in the case any exception is thrown or not. Such block is known as finally block.
The programmer creates this block to put the clean up code in it. For example if your program is communicating with database and in the middle of the try block there is any exception so the connection to the database will remain open. Such practices are not good ones. So we keep the code to close the connection to the database in the finally block, as it guarantees through this block that it will execute definately. Syntax of using finally block is as follows:
try { //code } catch(Exception e) { //handling code } finally { //clean up code }
For example,
int a=0; try { a = 10/a; //here exception will be thrown by the compiler System.out.println("Value of a is "+a); } catch(ArithmeticException ae) //catching the exception { System.out.println(ae.getMessage()); //printing the details of the exception } finally { System.out.println("Executing finally block"); //clean up code }
Output of the above code is as follows:
/ by zero Executing finally block
Note- There is only 1 case where the finally block will not execute i.e. when ever JVM shutdown before reaching finally block. JVM can be shutdown using System.exit(0). For example:
try { System.out.println("Try"); System.exit(0); } catch(Exception e) { System.out.println("Catch"); } finally { System.out.println("Finally"); }
Output of the above code will be:
Try