How to use Java multi-catch statement and re-throw exceptions of subtypes
- Details
- Written by Nam Ha Minh
- Last Updated on 17 August 2019   |   Print Email
1. Catching multiple exception types in Java
Consider the following Java code snippet:try {
doSomeRiskyThings();
} catch (IOException ex) {
LOGGER.log(ex);
} catch (SQLException ex) {
LOGGER.log(ex);
} catch (CustomException ex) {
LOGGER.log(ex);
}The method doSomeRiskyThings() may throws the three exception types: FileNotFoundException, IOException, and SQLException which are caught by the three separate catch blocks, but each block does the same thing (e.g. logging the exceptions and/or re-throwing them). This looks cluttered and duplicated and redundant. So in Java 7, we can catch all these three exception types in a single catch block like this:try {
doSomeRiskyThings();
} catch (IOException | SQLException | CustomException ex) {
LOGGER.log(ex);
}This improvement is called multi-catch statement, in which each exception is separated by the vertical bar symbol (|). Of course we can combine both regular catch and multi-catch like this:try {
doSomeRiskyThings();
} catch (IOException | SQLException ex) {
LOGGER.log(ex);
} catch (CustomException ex) {
// do another stuff with this custom exception
}There are two noteworthy rules regarding the multi-catch statement:- The exception variable is implicitly final, therefore we cannot assign the variable to different value within the catch block. For example, the following code snippet will give a compile error:
} catch (IOException | SQLException ex) { ex = new SQLException(); }The compiler will throw this error: multi-catch parameter ex may not be assigned - It is not allowed to specify two or more exceptions of a same hierarchy in the multi-catch statement. For example, the following code snippet will give a compile error because the FileNotFoundException is a subtype of the IOException:
} catch (FileNotFoundException | IOException ex) {
LOGGER.log(ex);
} The compiler will throw this error (no matter the order is): Alternatives in a multi-catch statement cannot be related by subclassing
The Exception class is the supertype of all exceptions, thus we also cannot write:} catch (IOException | Exception ex) {
LOGGER.log(ex);
} 1. Re-throwing subtypes of exception in Java
// this is illegal before Java SE 7
void doSomeRiskyThings() throws CustomException1, CustomException2 {
try {
if (errorNumber == 1) {
throw new CustomException1();
} else {
throw new CustomException2();
}
} catch (Exception ex) {
throw ex;
}
}Because the catch block re-throws the exception of type Exception, so the throws clause must declare to throw the Exception type, like this:// this is legal before Java SE 7
void doSomeRiskyThings() throws CustomException1, CustomException2, Exception {
try {
if (errorNumber == 1) {
throw new CustomException1();
} else {
throw new CustomException2();
}
} catch (Exception ex) {
throw ex;
}
} Or this:// this is legal before Java SE 7
void doSomeRiskyThings() throws Exception {
try {
if (errorNumber == 1) {
throw new CustomException1();
} else {
throw new CustomException2();
}
} catch (Exception ex) {
throw ex;
}
}However, since Java SE 7, we can declare to throw subtypes of the exceptions (in the throws clause) that are re-thrown in the catch blocks, like this:// this is legal since Java SE 7
void doSomeRiskyThings() throws CustomException1, CustomException2 {
try {
if (errorNumber == 1) {
throw new CustomException1();
} else {
throw new CustomException2();
}
} catch (Exception ex) {
throw ex;
}
}In other words, we can re-throw an exception which is a supertype of the exception types declared in the throwsclause. Related Tutorials:
- 5 Rules about Catching Exceptions in Java
- Java try-catch-finally construct
- How to throw exceptions in Java - the differences between throw and throws
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand Java Access Modifiers
- Understand how variables are passed in Java
- Understand Encapsulation in Java
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments