Prerequisite : Try-Catch Block in Java
In Java, it is possible that your program may encounter exceptions, for which the language provides try-catch statements to handle them. However, there is a possibility that the piece of code enclosed inside the ‘try’ block may be vulnerable to more than one exceptions. For example, take a look at the following sample code:
Output in case of either of the exceptions:
As you can see from the above code, there is a possibility that either of the two exceptions mentioned above can occur. In order to handle them both, the catch statement is made to accept any exception that can occur by passing a reference to the Exception class, that is the parent of all exception classes. However, this catch statement does the same thing for all kinds of exceptions.
Specify Custom Actions for different Exceptions
In order to specify custom actions in such cases, programmers usually put multiple catch statements, such as in the following example:
a) In case of ArithmeticException: Dividing by 0 b) In case of ArrayIndexOutOfBoundsException: That index doesn't exist
Specify Custom Actions for different Exceptions using instanceof
However, there is a way to do the same thing by using only one catch block. To do so, java provides an operator: instanceof.
By using this operator, we can specify custom actions for the different exceptions that occur. The following program demonstrates how:
a) In case of ArithmeticException: Can't divide by 0 b) In case of ArrayIndexOutOfBoundsException: This index doesn't exist in this array c) In case of no exception: No exception arose
By using the instanceof operator in exception handling, it is easy to achieve the aforementioned objective and at the same time, it makes your code less complicated. One thing that has to be noted here is that as soon as the try block encounters an exception, the control will directly jump to the catch block, to handle, thereby, preventing the rest of the body of the try block from executing. As a result, even though there may be possibility of more than one exceptions occurring, the try block will only throw one exception, before shifting control to the catch block.
- Try, catch, throw and throws in Java
- Java instanceof and its applications
- instanceof operator vs isInstance() method in Java
- Exceptions in Java
- Errors V/s Exceptions In Java
- Chained Exceptions in Java
- Checked vs Unchecked Exceptions in Java
- Built-in Exceptions in Java with examples
- throw and throws in Java
- Output of Java Programs | Set 41 (try-catch)
- Difference between throw and throws in Java
- Flow control in try catch finally in Java
- Output of Java Programs | Set 39 (throw keyword)
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.