How To Catch Multiple Exceptions
Maybe your like
In the previous tutorial, I have covered how to handle exceptions using try-catch blocks. In this guide, we will see how to handle multiple exceptions and how to write them in a correct order so that user gets a meaningful message for each type of exception.
Catching multiple exceptions
Lets take an example to understand how to handle multiple exceptions.
class Example{ public static void main(String args[]){ try{ int arr[]=new int[7]; arr[4]=30/0; System.out.println("Last Statement of try block"); } catch(ArithmeticException e){ System.out.println("You should not divide a number by zero"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Accessing array elements outside of the limit"); } catch(Exception e){ System.out.println("Some Other Exception"); } System.out.println("Out of the try-catch block"); } }Output:
You should not divide a number by zero Out of the try-catch blockIn the above example, the first catch block got executed because the code we have written in try block throws ArithmeticException (because we divided the number by zero).
Now lets change the code a little bit and see the change in output:
class Example{ public static void main(String args[]){ try{ int arr[]=new int[7]; arr[10]=10/5; System.out.println("Last Statement of try block"); } catch(ArithmeticException e){ System.out.println("You should not divide a number by zero"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Accessing array elements outside of the limit"); } catch(Exception e){ System.out.println("Some Other Exception"); } System.out.println("Out of the try-catch block"); } }Output:
Accessing array elements outside of the limit Out of the try-catch blockIn this case, the second catch block got executed because the code throws ArrayIndexOutOfBoundsException. We are trying to access the 11th element of array in above program but the array size is only 7.
What did we observe from the above two examples? 1. It is clear that when an exception occurs, the specific catch block (that declares that exception) executes. This is why in first example first block executed and in second example second catch. 2. Although I have not shown you above, but if an exception occurs in above code which is not Arithmetic and ArrayIndexOutOfBounds then the last generic catch handler would execute.
Lets change the code again and see the output:
class Example{ public static void main(String args[]){ try{ int arr[]=new int[7]; arr[10]=10/5; System.out.println("Last Statement of try block"); } catch(Exception e){ System.out.println("Some Other Exception"); } catch(ArithmeticException e){ System.out.println("You should not divide a number by zero"); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("Accessing array elements outside of the limit"); } System.out.println("Out of the try-catch block"); } }Output:
Compile time error: Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unreachable catch block for ArithmeticException. It is already handled by the catch block for Exception Unreachable catch block for ArrayIndexOutOfBoundsException. It is already handled by the catch block for Exception at Example.main(Example1.java:11)Why we got this error? This is because we placed the generic exception catch block at the first place which means that none of the catch blocks placed after this block is reachable. You should always place this block at the end of all other specific exception catch blocks.
❮ PreviousNext ❯Top Related Articles:
- Exception handling in Java with examples
- Nested try catch block in Java – Exception handling
- Java Exception Handling Examples
- How to Compile and Run your First Java Program
- What is case Keyword in Java
Tag » How To Catch Multiple Exceptions Java
-
Java Multiple Catch Block - GeeksforGeeks
-
Java Catch Multiple Exceptions, Rethrow Exception - DigitalOcean
-
Catching Multiple Exception Types And Rethrowing Exceptions With ...
-
Can I Catch Multiple Java Exceptions In The Same Catch Clause?
-
Java Catch Multiple Exceptions - Programiz
-
Java Multiple Catch Block Example - Javatpoint
-
Java 7 Catch Multiple Exceptions - Javatpoint
-
Catching Multiple Exceptions In Java 7
-
Java Catch Multiple Exceptions - CodeGym
-
Java 7 Catch Multiple Exceptions - W3schools.blog
-
Catch Multiple Exceptions Before & After Java 7 (examples)
-
Catching Multiple Exceptions In Java
-
Catching Multiple Exceptions In Java - More Than One Catch Block
-
Is It Possible To Catch Multiple Java Exceptions In Single Catch Block?