Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check which exception type was thrown in Java?

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.

like image 354
jean Avatar asked Dec 03 '14 20:12

jean


People also ask

Which exception is thrown by JVM?

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.

How do you handle different types of exceptions using multiple catch statements?

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.

Which type of exception is handled during compile time?

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.

Which exceptions can be thrown?

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.

When should I use checked exceptions in Java?

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.

What happens when an exception is thrown in Java?

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.

What are the different types of exception handling in Java?

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.

What is the use of throwable class in Java?

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..].


2 Answers

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 } 
like image 109
Erik Kaplun Avatar answered Sep 25 '22 01:09

Erik Kaplun


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.             }       } 
like image 29
anoopknr Avatar answered Sep 25 '22 01:09

anoopknr