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

[REPRODUCEABLE + PATCH] When scripting through Rhino, reported ScriptException lacks root cause

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P4 P4
    • tbd
    • 6, 7
    • core-libs
    • generic
    • generic, windows_xp

      This is a SUNBUG for https://bugs.openjdk.java.net/show_bug.cgi?id=100129

      Description From Guillaume C:

      Created an attachment (id=174) [details]
      demo of the problem

      When scripting through Rhino (e.g. selecting the "JavaScript" script engine),
      whenever an exception is thrown by a Java class (called by javascript), it is
      not exposed by the thrown ScriptException. Here's a demo:

      -=-=---=-=---=-=--
      import java.util.Hashtable;
      import javax.script.*;

      public class Test {

          private static class BuggyClass {
              private Hashtable store = new Hashtable();
              public BuggyClass() {
                  System.out.println("Creating BuggyClass instance...");
                  store.put(null, null); // BOOM
              }
          }

          public static void main(String[] args) {
              ScriptEngineManager manager = new ScriptEngineManager();
              ScriptEngine engine = manager.getEngineByName( "JavaScript" );
              try {
                  engine.eval("new Packages.Test.BuggyClass();");
              } catch ( Exception e ) {
                  Throwable t = (Throwable)e;
                  while (t != null) {
                      System.err.println("Exception: " + t.getClass());
                      t = t.getCause();
                  }
              }
          }
      }
      -=-=---=-=---=-=--

      This outputs:

      Creating BuggyClass instance...
      Exception: class javax.script.ScriptException

      The ScriptException object lacks a cause which would allow to obtain the
      underlying NullPointerException (with its stacktrace), located in the
      BuggyClass implementation; for deeper classes hierarchy than in this demo, it
      makes it nearly impossible to debug/locate where is the problem, although
      normally the "#getCause" of Throwable object makes it very efficient.

      With that modification to
      src/share/classes/com/sun/script/javascript/RhinoScriptEngine.java the cause
      would be present:

      -=-=---=-=---=-=--
      --- RhinoScriptEngine.java 2010-02-19 09:47:37.000000000 +0100
      +++ RhinoScriptEngine.java.patched 2010-02-19 17:45:00.000000000 +0100
      @@ -107,7 +107,9 @@
               } catch (RhinoException re) {
                   if (DEBUG) re.printStackTrace();
                   int line = (line = re.lineNumber()) == 0 ? -1 : line;
      - throw new ScriptException(re.toString(), re.sourceName(), line);
      + ScriptException se = new ScriptException(re.toString(),
      re.sourceName(), line);
      + se.initCause(re);
      + throw se;
               } catch (IOException ee) {
                   throw new ScriptException(ee);
               } finally {
      @@ -181,7 +183,9 @@
               } catch (RhinoException re) {
                   if (DEBUG) re.printStackTrace();
                   int line = (line = re.lineNumber()) == 0 ? -1 : line;
      - throw new ScriptException(re.toString(), re.sourceName(), line);
      + ScriptException se = new ScriptException(re.toString(),
      re.sourceName(), line);
      + se.initCause(re);
      + throw se;
               } finally {
                   cx.exit();
               }
      -=-=---=-=---=-=--

      And the above example program would output:

      Creating BuggyClass instance...
      Exception: class javax.script.ScriptException
      Exception: class org.mozilla.javascript.WrappedException
      Exception: class java.lang.NullPointerException

      making is much more useful!

      This was checked against lastest stable, 1.6.0_18.

      Comment #1 From Guillaume C:

      Created an attachment (id=175) [details]
      proposed patch to RhinoScriptEngine.java

            sundar Sundararajan Athijegannathan
            tbell Tim Bell
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: