How to access Java exception that causes ScriptException using JSR-223
Asked Answered
R

1

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.

Rimple answered 25/10, 2011 at 12:37 Comment(2)
Does Throwable.getCause() not do what you are asking?Cardcarrying
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.Ratify
O
0

have you tried e.getCause()?

Open answered 25/10, 2011 at 12:43 Comment(1)
yes, but it stops at the JavaScript, it doesn't contain the cause from the Java codeRimple

© 2022 - 2024 — McMap. All rights reserved.