Wednesday, September 24, 2014

catching multiple exception in Java: java 1.7 update

2:22 PM



Did you know from java 1.7 you can catch multiple exceptions in a single catch block ?

Until java 1.6 if we wanted to catch multiple exceptions in a single java program we have to write multiple catch blocks.

For Example in Java 1.6 environment

class ExceptionTest
{

                public static void main(String[] args)
                {
                                try
                                {
                                                int num1=Integer.parseInt(args[0]);
                                                int num2=Integer.parseInt(args[1]);
                                                int result=num1/num2;
                                }
                                catch (ArithmeticException e)
                                {
                                                System.out.println(e.getMessage());
                                } 
                                catch (ArrayIndexOutOfBoundsException e)
                                {
                                                System.out.println(e.getMessage());
                                } 
                                catch (NumberFormatException e)
                                {
                                                System.out.println(e.getMessage());
                                }
                               
                }
}

This leads to duplicity of code making program lengthy and quite tough to read.

But from java 1.7 we can catch multiple exceptions in a single catch block. Above example code can be rewritten in Java 1.7 and higher version as. 


class ExceptionTest
{

                public static void main(String[] args)
                {
                                try
                                {
                                                int num1=Integer.parseInt(args[0]);
                                                int num2=Integer.parseInt(args[1]);
                                                int result=num1/num2;
                                }
                                catch (ArithmeticException |NumberFormatException |ArrayIndexOutOfBoundsException e)
                                {
                                                System.out.println(e.getMessage());
                                }                             
                }
}

Rules to catch multiple exceptions

  1. Exception type parameters must be separated by a pipe symbol.
  2. We are not allowed to pass alternate exception in a multi exception catch block. Violation it will lead to a compile time error.
  3. By default variable in multi exception catch block is implicitly final, hence no other assignment on the same variable are allowed. Violation the rule will give a compile time error.
     

      NOTE
·         Byte code generated for above example using java 1.6 and java 1.7 + version have no changes.


0 comments :

Post a Comment

 

© 2014 DieHardTechy . All rights resevered. Designed by Diehardtechy