Multiple catch blocks are used in java for handling different types of exceptions. Single catch blocks are defined within Exception class type as it is super class for all type of exceptions. We can handle more than one type of exception in a catch block. In java to stop abnormal termination, we use Exception Handling. To handle exceptions we use try-and-catch blocks. Generally for a particular type of Exception we need to define a special catch blocks, i.e. we use multiple catch blocks to handle different type of exceptions. Doing so, a single catch block can handle only specified type of exception and its subclass exceptions type. If we want to handle any type of exception in a single catch block, then, we can define a single catch block with Exception class type, because Exception class is super class for all type of exceptions, and super class reference variable can hold the object of any of its sub class. The following catch block can handle all type of exceptions. catch(Exception e) { System.out.println(“Exception caught:”+e); } Note: When we want to define multiple-catch blocks then we should remember that, the exception super class should come after any of its subclass otherwise the complier will generate error. The following code will generates compile time error. try { ….. } catch(ArithmeticException e) { System.out.println(“Dividing by zero is an Exception”+ e); } catch(Exception e) { System.out.println(“Exception caught:”+e); } catch(NegativeArraySizeException e) { System.out.println(“ArrayException”+ e); } In the above code the catch block which can hold all type of exceptions is placed in the middle of the catch block sequence which leads to an error called “Unreachable code Error”. To overcome this problem we have to write such catch block after all catch blocks has defined, which is shown as below. try { ….. } catch(ArithmeticException e) { System.out.println(“Dividing by zero is an Exception”+ e); } catch(NegativeArraySizeException e) { System.out.println(“ArrayException”+ e); } catch(Exception e) { System.out.println(“Exception caught:”+e); } There may be some situations where some set of Exceptions has same handling statements, for such cases we can use multi-catch block. Now let us see how a single catch block can handle more than one Exception type. Syntax for such catch blocks is as follow. catch(ExceptionType1 ExceptionType2 e) { . //Body of multi-catch block. . . } In multi-catch block we can handle more than one type of Exception and each exception should be separated with a Bitwise-OR ( ) operator. Before going to see an example on multi-catch block, let us see the following example. try { ….. } catch(ArithmeticException e) { System.out.println(“Dividing by zero is an Exception”+ e); } catch(ArrayIndexOutOfBoundsException e) { System.out.println(“ArrayException”+ e); } catch(NegativeArraySizeException e) { System.out.println(“ArrayException”+ e); } catch(Exception e) { System.out.println(); } try { ….. } catch(ArithmeticException e) { System.out.println(“Dividing by zero is an Exception”+ e); } catch(ArrayIndexOutOfBoundsException NegativeArraySizeException e) { System.out.println(“ArrayException”+ e); } catch(Exception e) { System.out.println(); } In the above code two catch blocks have same set of statements. For such cases instead of writing two catch blocks we can define only one catch block as follow. Using this type of multi-catch block we can save the time and space. This gives good readability and writability for the programmer. Now lets us see the complete program on multi-catch block. class MutiCatchBlockDemo { public static void main(String args[]) { try { int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); int c=a/b; System.out.println("c="+c); //another array declaration int x[]=new int[-4];//NegativeArraySizeException generates here. } catch(ArithmeticException e) { System.out.println("Dividing by zero is an Exception"+ e); } catch(ArrayIndexOutOfBoundsException NegativeArraySizeException e) { System.out.println("Multi-Catch block executed.."); System.out.println("ArrayException"+ e); } catch(Exception e) { System.out.println("Exception"+ e); } }//End of main() method. }//End of class block The following code reads the data from command line argument. Compilation: C:\>javac MutiCatchBlockDemo.java Execution1: If no command line arguments are passed. C:\>java MutiCatchBlockDemo Multi-Catch block executed.. ArrayExceptionjava.lang.ArrayIndexOutOfBoundsException: 0 Execution2: If only one command line argument is passed. C:\>java MutiCatchBlockDemo 1 Multi-Catch block executed.. ArrayExceptionjava.lang.ArrayIndexOutOfBoundsException: 1 Execution3: If two command line arguments are passed. C:\>java MutiCatchBlockDemo 6 3 c=2 Multi-Catch block executed.. ArrayExceptionjava.lang.NegativeArraySizeException Execution4: If the second command line arguments is zero. C:\>java MutiCatchBlockDemo 6 0 Dividing by zero is an Exceptionjava.lang.ArithmeticException: / by zero Execution5: If any one of the command line arguments is non integer C:\>java MutiCatchBlockDemo 6 3b Exceptionjava.lang.NumberFormatException: For input string: "3b" Note: In the Execution 1,2 and 3 the multi-catch block is executed. For both ArithmeticException and NegativeArraySizeException the single multi-catch block is executed. You can see reasonable no of questions on this topic in the certification exam. Practice through some good OCPJP and see the type of questions that appear in the actual exam. The author recommends Whizlabs for preparing for certifications like Oracle Java, Cisco and Project Management.
Related Articles -
whizlabs.com,
|