Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4909760

Extended syntax for catch statements

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 1.4.2, 6
    • specification
    • x86
    • windows_xp

      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)
      ======================================================================

            abuckley Alex Buckley
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: