2 Replies Latest reply on Aug 28, 2005 3:41 PM by gpothier

    CannotCompileException / root cause

    gpothier

      The CannotCompileException class has a "cause" field that permit to store another exception that was the cause of the exception.
      The printStackTrace method are overridden so that the cause exception's stack trace is also printed, if there is one. But there is a problem: if the CannotCompileException itself is the cause of another exception, the trace of CannotCompileException's cause is not printed.

      I'm wondering, why is there a "cause" field? The standard Java Exception class already handles the idea of cause exceptions, is there a reason for it not to be used?

      Proposed patch:

      Index: CannotCompileException.java
      ===================================================================
      RCS file: /cvsroot/jboss/javassist/src/main/javassist/CannotCompileException.java,v
      retrieving revision 1.10
      diff -u -r1.10 CannotCompileException.java
      --- CannotCompileException.java 20 Jun 2005 17:05:57 -0000 1.10
      +++ CannotCompileException.java 24 Aug 2005 17:53:03 -0000
      @@ -22,7 +22,6 @@
      */
      public class CannotCompileException extends Exception {
      private String message;
      - private Throwable cause;

      public String getReason() {
      if (message != null)
      @@ -39,7 +38,6 @@
      public CannotCompileException(String msg) {
      super(msg);
      message = msg;
      - cause = null;
      }

      /**
      @@ -49,9 +47,8 @@
      * @param e the cause.
      */
      public CannotCompileException(Throwable e) {
      - super("by " + e.toString());
      + super("by " + e.toString(), e);
      message = null;
      - cause = e;
      }

      /**
      @@ -62,8 +59,7 @@
      * @param e the cause.
      */
      public CannotCompileException(String msg, Throwable e) {
      - this(msg);
      - cause = e;
      + super(msg, e);
      }

      /**
      @@ -96,25 +92,4 @@
      this("invalid class format: " + name, e);
      }

      - /**
      - * Prints this exception and its backtrace.
      - */
      - public void printStackTrace(java.io.PrintWriter w) {
      - super.printStackTrace(w);
      - if (cause != null) {
      - w.println("Caused by:");
      - cause.printStackTrace(w);
      - }
      - }
      -
      - /**
      - * Prints this exception and its backtrace.
      - */
      - public void printStackTrace(java.io.PrintStream w) {
      - super.printStackTrace(w);
      - if (cause != null) {
      - w.println("Caused by:");
      - cause.printStackTrace(w);
      - }
      - }
      }


      Regards,
      Guillaume Pothier