United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6943289 : Project Coin: Improved Exception Handling for Java (aka 'multicatch')

Details
Type:
Enhancement
Submit Date:
2010-04-13
Status:
Closed
Updated Date:
2013-04-24
Project Name:
JDK
Resolved Date:
2011-03-08
Component:
tools
OS:
generic
Sub-Component:
javac
CPU:
unknown
Priority:
P2
Resolution:
Fixed
Affected Versions:
7
Fixed Versions:

Related Reports
Relates:
Relates:
Relates:
Relates:
Relates:
Relates:

Sub Tasks

Description
This proposal is structured into two different parts:

*) Catching multiple exception types: A single catch clause can now catch
   more than one exception types, enabling a series of otherwise identical
   catch clauses to be written as a single catch clause.

*) Improved checking for rethrown exceptions:  Previously, rethrowing an
   exception was treated as throwing the type of the catch parameter.  Now,
   when a catch parameter is declared final, rethrowing the exception is known
   statically to throw only those checked exception types that were thrown in
   the try block, are a subtype of the catch parameter type, and not caught in
   preceding catch clauses.

EXAMPLE (multicatch):

before:

try {
    doWork(file);
} catch (IOException ex) {
    logger.log(ex);
} catch (SQLException ex) {
    logger.log(ex);
}

after:

try {
    doWork(file);
} catch (final IOException|SQLException ex) {
    logger.log(ex);
}

EXAMPLE (rethrow)

before

void m() throws IOException, SQLException {
try {
   doWork(file);
} catch (IOException) {
    logger.log(ex);
} catch (SQLException ex) {
    logger.log(ex);
    throw ex; //rethrows IOException,SQLException
}
}

after:

void m() throws SQLException {
try {
   doWork(file);
} catch (IOException) {
    logger.log(ex);
} catch (final SQLException ex) {
    logger.log(ex);
    throw ex; //rethrows SQLException
}
}

                                    

Comments
SUGGESTED FIX

A webrev of this fix is available at the following URL:
http://hg.openjdk.java.net/jdk7/tl/langtools/rev/a6f2911a7c55
                                     
2010-05-12
EVALUATION

A fine feature.
                                     
2010-05-04



Hardware and Software, Engineered to Work Together