How can I determine which type of exception was caught, if an operation catches multiple exceptions?
This example should make more sense:
try { int x = doSomething(); } catch (NotAnInt | ParseError e) { if (/* thrown error is NotAnInt */) { // line 5 // printSomething } else { // print something else } }
On line 5, how can I check which exception was caught?
I tried if (e.equals(NotAnInt.class)) {..}
but no luck.
NOTE: NotAnInt
and ParseError
are classes in my project that extend Exception
.
JVM Exceptions − These are exceptions/errors that are exclusively or logically thrown by the JVM. Examples: NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException. Programmatic Exceptions − These exceptions are thrown explicitly by the application or the API programmers.
The Try Block If your code throws more than one exception, you can choose if you want to: use a separate try block for each statement that could throw an exception or. use one try block for multiple statements that might throw multiple exceptions.
Checked exceptions A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions.
Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment.
You should use checked exceptions for all exceptional events that you can anticipate and that a well-written application should be able to handle. A checked exception extends the Exception class. A method that throws a checked exception or that calls a method that specifies a checked exception needs to either specify or handle it.
When a method throws an exception object, the runtime searches the call stack for a piece of code that handles it. I will get into more details about exception handling in the How to Handle an Exception section of this post. Java supports checked and unchecked exceptions.
Customized Exception Handling : Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Briefly, here is how they work. Program statements that you think can raise exceptions are contained within a try block. If an exception occurs within the try block, it is thrown.
Throwable class provides some of the super useful method which will be used to handle the exception This method also returns the exception message string [only the name of the exception] but in the local language of the user [French, Chinese, etc..].
If you can, always use separate catch
blocks for individual exception types, there's no excuse to do otherwise:
} catch (NotAnInt e) { // handling for NotAnInt } catch (ParseError e) { // handling for ParseError }
...unless you need to share some steps in common and want to avoid additional methods for reasons of conciseness:
} catch (NotAnInt | ParseError e) { // a step or two in common to both cases if (e instanceof NotAnInt) { // handling for NotAnInt } else { // handling for ParseError } // potentially another step or two in common to both cases }
however the steps in common could also be extracted to methods to avoid that if
-else
block:
} catch (NotAnInt e) { inCommon1(e); // handling for NotAnInt inCommon2(e); } catch (ParseError e) { inCommon1(e); // handling for ParseError inCommon2(e); } private void inCommon1(e) { // several steps // common to // both cases } private void inCommon2(e) { // several steps // common to // both cases }
If Multiple throws
are happening in a single catch()
then to recognize which Exception , you could use instanceof
operator.
The java instanceof
operator is used to test whether the object is an instance of the specified type (class or subclass or interface).
Try this code :-
catch (Exception e) { if(e instanceof NotAnInt){ // Your Logic. } else if if(e instanceof ParseError){ //Your Logic. } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With