United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-4909760 : Extended syntax for catch statements

Details
Type:
Enhancement
Submit Date:
2003-08-20
Status:
Closed
Updated Date:
2007-01-19
Project Name:
JDK
Resolved Date:
2007-01-19
Component:
specification
OS:
windows_xp
Sub-Component:
language
CPU:
x86
Priority:
P5
Resolution:
Duplicate
Affected Versions:
1.4.2,6
Fixed Versions:

Related Reports
Duplicate:
Duplicate:

Sub Tasks

Description
Name: jl125535			Date: 08/20/2003


A DESCRIPTION OF THE REQUEST :
Extend the syntax of the catch statement to allow catching of multiple exception types in a single statement.

JUSTIFICATION :
There are times when one wants to catch multiple exception types and deal with them all in the same way, but it is not appropriate to catch some common supertype (such as Exception). An example might be using Class.forName().newInstance(). This statement throws ClassNotFoundException, InstantiationException and IllegalAccessException (and a few Errors too). How many times have you found yourself writing:

  try {
    Object o = Class.forName("com.blah").newInstance();
  } catch (ClassNotFoundException cnfe) {
    throw new HighLevelException(cnfe);
  } catch (InstantiationException ie) {
    throw new HighLevelException(ie);
  } catch (IllegalAccessException iae) {
    throw new HighLevelException(iae);
  }

Sometime it is inappropriate to catch (Exception e) in these cases as there may be other exceptions (from other statements in the try block) which you wish to propogate or otherwise handle differently.

It would be useful to say:

  try {
    Object o = Class.forName("com.blah").newInstance();
  } catch (ClassNotFoundException,
           InstantiationException,
           IllegalAccessException e) {
    throw new HighLevelException(e);
  }

The actual compile time type of 'e' would be the closest common supertype of all exception types listed (maybe Exception, or even Throwable).
(Incident Review ID: 190874) 
======================================================================

                                    

Comments
EVALUATION

This is a request for a minor and inconsequential syntactic sugar. 
I don't believe this even a common occurrence, and it may possibly encourage
laziness and bad style.

###@###.### 2003-08-20
                                     
2003-08-20



Hardware and Software, Engineered to Work Together