7

I'm executing Javascripts using the JSR-223 script engine built into JRE6. The Javascripts are able to access Java code and objects. When an exception is thrown from the Java code that is executed from a JavaScript, the ScriptEngine will throw a ScriptException.

I want to be able to access the Java exception that caused the Javascript to throw an exception.

From the Javascript, I can catch the exception and look at the javaException field of the exception:

try
{
  .
}
catch (e)
{
  e.javaException.printStackTrace();
}

However, I don't have control of the Javascript, only the execution of them from the ScriptEngine. Is there a way to grab the "inner" Java exception from the ScriptException?

I believe if I was using Rhino, the RhinoException would have a Java exception available. The RhinoException is not available from the JSR-223 API.

Dave H
  • 547
  • 1
  • 5
  • 16
  • Does [`Throwable.getCause()`](http://download.oracle.com/javase/7/docs/api/java/lang/Throwable.html#getCause%28%29) not do what you are asking? – Andrew Thompson Oct 25 '11 at 12:47
  • 1
    In a ScriptException caused on a Java level, a WrappedException is used by Rhino internally. _However_, the "cause" set to the script exception is itself. There is no way to get the wrapped exception and see where exactly the Java level exception occured, not even using reflection over the non-JSR rhino classes. This seems to be a severe bug. – pdinklag Mar 13 '13 at 08:27

1 Answers1

0

have you tried e.getCause()?

AlexR
  • 114,158
  • 16
  • 130
  • 208