JDK-4909760 : Extended syntax for catch statements
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.2,6
  • Priority: P5
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-08-20
  • Updated: 2007-01-19
  • Resolved: 2007-01-19
Related Reports
Duplicate :  
Duplicate :  
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
20-08-2003