This page covers the following topics:
In Java SE 7 and later, a singlecatch 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.
Consider the following example, which contains duplicate code in each of thecatch blocks:
catch (IOException ex) { logger.log(ex); throw ex;catch (SQLException 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 variableex has different types.
The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:
catch (IOException|SQLException ex) { logger.log(ex); throw ex;}Thecatch clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (|).
Note: If acatch block handles more than one exception type, then thecatch parameter is implicitlyfinal. In this example, thecatch parameterex isfinal and therefore you cannot assign any values to it within thecatch block.
Bytecode generated by compiling acatch block that handles multiple exception types will be smaller (and thus superior) than compiling manycatch blocks that handle only one exception type each. Acatch block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers.
The Java SE 7 compiler performs more precise analysis of rethrown exceptions than earlier releases of Java SE. This enables you to specify more specific exception types in thethrows clause of a method declaration.
Consider the following example:
static class FirstException extends Exception { } static class SecondException extends Exception { } public void rethrowException(String exceptionName) throws Exception { try { if (exceptionName.equals("First")) { throw new FirstException(); } else { throw new SecondException(); } } catch (Exception e) { throw e; } }This examples'stry block could throw eitherFirstException orSecondException. Suppose you want to specify these exception types in thethrows clause of therethrowException method declaration. In releases prior to Java SE 7, you cannot do so. Because the exception parameter of thecatch clause,e, is typeException, and the catch block rethrows the exception parametere, you can only specify the exception typeException in thethrows clause of therethrowException method declaration.
However, in Java SE 7, you can specify the exception typesFirstException andSecondException in thethrows clause in therethrowException method declaration. The Java SE 7 compiler can determine that the exception thrown by the statementthrow e must have come from thetry block, and the only exceptions thrown by thetry block can beFirstException andSecondException. Even though the exception parameter of thecatch clause,e, is typeException, the compiler can determine that it is an instance of eitherFirstException orSecondException:
public void rethrowException(String exceptionName) throwsFirstException, SecondException { try { // ... } catch (Exception e) { throw e; } }This analysis is disabled if thecatch parameter is assigned to another value in thecatch block. However, if the catch parameter is assigned to another value, you must specify the exception typeException in thethrows clause of the method declaration.
In detail, in Java SE 7 and later, when you declare one or more exception types in acatch clause, and rethrow the exception handled by thiscatch block, the compiler verifies that the type of the rethrown exception meets the following conditions:
try block is able to throw it.catch blocks that can handle it.catch clause's exception parameters.The Java SE 7 compiler allows you to specify the exception typesFirstException andSecondException in thethrows clause in therethrowException method declaration because you can rethrow an exception that is a supertype of any of the types declared in thethrows.
In releases prior to Java SE 7, you cannot throw an exception that is a supertype of one of thecatch clause's exception parameters. A compiler from a release prior to Java SE 7 generates the error, "unreported exceptionException; must be caught or declared to be thrown" at the statementthrow e. The compiler checks if the type of the exception thrown is assignable to any of the types declared in thethrows clause of therethrowException method declaration. However, the type of the catch parametere isException, which is a supertype, not a subtype, ofFirstException andSecondException.