Proposal: Improved Exception Handling for Java (original) (raw)

Neal Gafter neal at gafter.com
Fri Feb 27 21:22:44 PST 2009


Improved Exception Handling for Java*AUTHOR(S):*Neal M Gafter OVERVIEW FEATURE SUMMARY:

MAJOR ADVANTAGE:

MAJOR BENEFIT:Greatly simplifies writing and maintaining code where intercepting or processing exceptions is common. MAJOR DISADVANTAGE:One-time implementation cost for adding the features to the compiler. Longer language specification in describing the behavior. ALTERNATIVES:

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

} ADVANCED EXAMPLE:Show advanced usage(s) of the feature. DETAILS SPECIFICATION: The grammar of Java is extended to allow a series of exception types, separated by the "OR" operator symbol, to be used in a catch clause:

CatchClause: catch ( CatchFormalParameter ) *Block CatchFormalParameter: VariableModifiers CatchType VariableDeclaratorId CatchType: DisjunctionType DisjunctionType: Type Type | DisjunctionType *

The type system is affected as follows: For the purpose of type checking, a catch parameter declared with a disjunction has type lub(t1, t2, ...) [JLS3 15.12.2.5<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#301183>]. For the purpose of exception checking [JLS3 11.2<http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html#11.2>], a throw statement [JLS3 11.2.2<http://java.sun.com/docs/books/jls/third_edition/html/exceptions.html#11.2.2>] that rethrows a final catch parameter is treated as throwing precisely those exception types that

To avoid the need to add support for general disjunctive types, but leaving open the possibility of a future extension along these lines, a catch parameter whose type has more than one disjunct is required to be declared * final*. COMPILATION:A catch clause is currently compiled (before this change) to an entry in an exception table that specifies the type of the exception and the address of the code for the catch body. To generate code for this new construct, the compiler would generate an entry in the exception table for each type in the exception parameter's list of types. TESTING:The feature can be tested by compiling and running programs that exercise the feature. LIBRARY SUPPORT:No. REFLECTIVE APIS:No reflective API changes are required. OTHER CHANGES:It would be desireable, at the same time that this change is made, to update the non-public Tree API that can be used with APT to express the syntax extension. MIGRATION:None required. However, it would be easy to detect a series of otherwise identical catch clauses for different types and collapse them to a single catch clause. COMPATIBILITY BREAKING CHANGES:Joe Darcy observes that the following program compiles before this change, but not after:

try {

However

EXISTING PROGRAMS:Except as above, none. REFERENCES EXISTING BUGS:No existing bugs that I am aware of. URL FOR PROTOTYPE (optional):An implementation of disjunctive catch parameters, but without special handling for final catch parameters:

See also



More information about the coin-dev mailing list