JAVA7 – Catching Multiple Exception Types and Rethrowing Exceptions

Catching Multiple Exception Types and Rethrowing Exceptions

Handling More Than One Type of Exception

In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication.

Consider the following example, which contains duplicate code in each of the catch blocks:

catch (ParseException ex) {
logger.log(ex);
throw ex;
catch (IOException ex) {
logger.log(ex);
throw ex;
}

In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable ex has different types.

The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:

catch (ParseException|IOException ex) {
logger.log(ex);
throw ex;
}

The catch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).

Note: If a catch block handles more than one exception type, then the catch parameter is implicitly final. In this example, the catch parameter ex is final and therefore you cannot assign any values to it within the catch block.

Rethrowing Exceptions

The Java SE 7 compiler performs more precise analysis of rethrown exceptions. It enables you to specify more specific exception types in the throws clause of a method declaration.

Consider the following example:

static class ExceptionOne extends Exception { }
static class ExceptionTwo extends Exception { }

public void rethrowMyException(String exceptionName) throws Exception {
try {
if (exceptionName.equals(“One”)) {
throw new ExceptionOne();
} else {
throw new ExceptionTwo();
}
} catch (Exception e) {
throw e;
}
}

Here try block could throw either ExceptionOne or ExceptionTwo. Suppose you want to specify these exception types in the throws clause of the rethrowException method declaration. In releases prior to Java SE 7, you cannot do so. Because the exception parameter of the catch clause, e, is type Exception, and the catch block rethrows the exception parameter e, you can only specify the exception type Exception in the throws clause of the rethrowException method declaration.

However, in Java SE 7, you can specify the exception types ExceptionOne and ExceptionTwo in the throws clause in the rethrowException method declaration. The Java SE 7 compiler can determine that the exception thrown by the statement throw e must have come from the try block, and the only exceptions thrown by the try block can be ExceptionOne and ExceptionTwo. Even though the exception parameter of the catch clause, e, is type Exception, the compiler can determine that it is an instance of either ExceptionOne or ExceptionTwo:

public void rethrowMyException(String exceptionName)
throws ExceptionOne, ExceptionTwo {
try {
// …
}
catch (Exception e) {
throw e;
}
}

This analysis is disabled if the catch parameter is assigned to another value in the catch block. However, if the catch parameter is assigned to another value, you must specify the exception type Exception in the throws clause of the method declaration.

In detail, in Java SE 7 and later, when you declare one or more exception types in a catch clause, and rethrow the exception handled by this catch block, the compiler verifies that the type of the rethrown exception meets the following conditions:

The try block is able to throw it.
There are no other preceding catch blocks that can handle it.
It is a subtype or supertype of one of the catch clause’s exception parameters.

Gopal Das
Follow me

Gopal Das

Founder at GopalDas.Org
He is a technology evangelist, Salesforce trainer, blogger, and working as a Salesforce Technical Lead. After working in Java based project implementation, he jumped to the Salesforce system on a whim and never looked back. He fell in love with Salesforce’s flexibility, scalability, and power. He expanded his knowledge of the platform and became a Certified App Builder, Administrator, Platform Developer I, SalesCloud Consultant while leading the Salesforce implementation and technology needs. He has worked in a wide variety of applications/services like desktop, web and mobile applications.
Gopal Das
Follow me

Leave a Reply

Your email address will not be published. Required fields are marked *