Is it possible in Java to catch two exceptions in the same catch block? [duplicate]
I need to catch two exceptions because they require the same handling logic. I would like to do something like:
catch (Exception e, ExtendsRuntimeException re) {
// common logic to handle both exceptions
}
Is it possible to avoid duplicating the handler code in each catch block?
Java 7 and later
Multiple-exception catches are supported, starting in Java 7.
The syntax is:
try {
// stuff
} catch (Exception1 | Exception2 ex) {
// Handle both exceptions
}
The static type of ex
is the most specialized common supertype of the exceptions listed. There is a nice feature where if you rethrow ex
in the catch, the compiler knows that only one of the listed exceptions can be thrown.
Java 6 and earlier
Prior to Java 7, there are ways to handle this problem, but they tend to be inelegant, and to have limitations.
Approach #1
try {
// stuff
} catch (Exception1 ex) {
handleException(ex);
} catch (Exception2 ex) {
handleException(ex);
}
public void handleException(SuperException ex) {
// handle exception here
}
This gets messy if the exception handler needs to access local variables declared before the try
. And if the handler method needs to rethrow the exception (and it is checked) then you run into serious problems with the signature. Specifically, handleException
has to be declared as throwing SuperException
... which potentially means you have to change the signature of the enclosing method, and so on.
Approach #2
try {
// stuff
} catch (SuperException ex) {
if (ex instanceof Exception1 || ex instanceof Exception2) {
// handle exception
} else {
throw ex;
}
}
Once again, we have a potential problem with signatures.
Approach #3
try {
// stuff
} catch (SuperException ex) {
if (ex instanceof Exception1 || ex instanceof Exception2) {
// handle exception
}
}
If you leave out the else
part (e.g. because there are no other subtypes of SuperException
at the moment) the code becomes more fragile. If the exception hierarchy is reorganized, this handler without an else
may end up silently eating exceptions!
Java <= 6.x just allows you to catch one exception for each catch block:
try {
} catch (ExceptionType name) {
} catch (ExceptionType name) {
}
Documentation:
Each catch block is an exception handler and handles the type of exception indicated by its argument. The argument type, ExceptionType, declares the type of exception that the handler can handle and must be the name of a class that inherits from the Throwable class.
For Java 7 you can have multiple Exception caught on one catch block:
catch (IOException|SQLException ex) {
logger.log(ex);
throw ex;
}
Documentation:
In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.
Reference: http://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
If you aren't on java 7, you can extract your exception handling to a method - that way you can at least minimize duplication
try {
// try something
}
catch(ExtendsRuntimeException e) { handleError(e); }
catch(Exception e) { handleError(e); }
For Java < 7 you can use if-else along with Exception:
try {
// common logic to handle both exceptions
} catch (Exception ex) {
if (ex instanceof Exception1 || ex instanceof Exception2) {
}
else {
throw ex;
// or if you don't want to have to declare Exception use
// throw new RuntimeException(ex);
}
}
Edited and replaced Throwable with Exception.