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

Throwable.initCause javadoc is misleading

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta2
    • generic
    • generic

      The following new example code in the Throwable javadoc for throwing a
      "legacy throwable" with a cause:

      * <tt>Throwable</tt>. For example:
      * <pre>
      * try {
      * lowLevelOp();
      * } catch LowLevelException(le) {
      * throw new HighLevelException().initCause(le); // Legacy constructor
      * }
      * </pre>

      will very rarely be directly mimic-able in practice, because it will
      only compile if this try block is within another try block that catches
      Throwable or a method that declares that it throws Throwable.

      So just to avoid people making the same mistake that I just did by
      blindly mimicking the example code and getting the obvious compiler
      error, it might be useful to augment the example code with the
      appropriate cast:

      throw (HighLevelException) new HighLevelException().initCause(le);

      or:

      HighLevelException he = new HighLevelException();
      he.initCause(le);
      throw he;

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: