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

More Throwables should support nested Throwables

XMLWordPrintable

    • tiger
    • generic, x86
    • generic, linux, windows_2000



      Name: gm110360 Date: 03/24/2003


      A DESCRIPTION OF THE REQUEST :
      More Throwables should support nested Throwables. The root Throwable class has some very nice API for this support, but subclasses rarely take advantage of it. Constructors that tack the cause in the argument encourage developers to use it, and write better code.

      JUSTIFICATION :
      I am currently catching a declared Exception from a library. This libary exception indicates an illegal state. I'd like to throw an IllegalStateException, but have to jump through some hoops because IllegalStateException's constructor does not take a nested Throwable.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      I'd like to use the code

          throw new IllegalStateException("bad state details",myException);
      If I use that code, it won't compile.

      ---------- BEGIN SOURCE ----------
      A test case containing

          throw new IllegalStateException("bad state details",new Exception("test"));

      should reproduce the problem nicely.
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      I can use

          Throwable thrown = new IllegalStateException("message");
          thrown.initCause(cause);
          throw thrown;

      but the shorter API will encourage others to nest exceptions much better.
      (Review ID: 182957)
      ======================================================================

      Name: nt126004 Date: 05/15/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_02"

      A DESCRIPTION OF THE PROBLEM :
        To my surprise in JDK 1.4.1_02 some exceptions like
      IllegalArgumentException, IllegalStateException
      do not support constructors for exception chaining,
      e.g. IllegalStateException(Throwable cause)

      The method Throwable.initCause(Throwable)
      however allows the setting of the cause.

      The result is ugly code if I want to set the cause:
      instead of
      throw new new IllegalStateException(theCause);

      I must write:
      IllegalStateException ex = new IllegalStateException();
      ex.initCause(theCause);
      throw ex;


      Or is this omission of constructors on purpose for some reason?
      (Review ID: 185824)
      ======================================================================

            jjb Josh Bloch
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: