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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Try, catch, throw and throws in Java
- Java instanceof and its applications
- instanceof operator vs isInstance() method in Java
- Exceptions in Java
- Chained Exceptions in Java
- Errors V/s Exceptions In Java
- throw and throws in Java
- Built-in Exceptions in Java with examples
- Checked vs Unchecked Exceptions in Java
- Difference between throw and throws in Java
- Output of Java Programs | Set 41 (try-catch)
- Output of Java Programs | Set 39 (throw keyword)
- Flow control in try catch finally in Java
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java lang.Long.highestOneBit() 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.