I am creating a game engine using Java and Mozilla Rhino, and I want all errors to call one function and provide it with the error message, like
...
} catch(Exception e) {
Abort(e);
}
...
public void Abort(str) {
System.out.println("Script error in "current_script_name+" line: "+line_number\n\n"+e);
}
This is easy for RhinoException, but I want to have the same thing for others, like IOException.
It depends a bit how the exceptions are being thrown, so I'll need to take a guess. And it depends on the optimization level you're using to execute Rhino.
I am guessing that exceptions are being thrown out of native Java code (i.e., you are not using throw new Packages.java.io.IOException("...")
). In that case, you can use printStackTrace() to figure it out. Here's a little script (named test.jsh.js
, which you will see in the stack trace) you can run in the Rhino shell:
try {
// foo does not exist
var stream = new Packages.java.io.FileInputStream("foo");
} catch (e) {
e.rhinoException.printStackTrace();
}
... and its output:
$ java -jar $(cygpath -w /opt/java/rhino/1.7R2/js.jar) -opt -1 test.jsh.js org.mozilla.javascript.WrappedException: Wrapped java.io.FileNotFoundException: foo (The system cannot find the file specified) (test.jsh.js#4) at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1773) at org.mozilla.javascript.MemberBox.newInstance(MemberBox.java:202) at org.mozilla.javascript.NativeJavaClass.constructSpecific(NativeJavaClass.java:281) at org.mozilla.javascript.NativeJavaClass.construct(NativeJavaClass.java:200) at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:3377) at script(test.jsh.js:4) at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2487) at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:164) at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:398) at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3065) at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:175) at org.mozilla.javascript.tools.shell.Main.evaluateScript(Main.java:564) at org.mozilla.javascript.tools.shell.Main.processFileSecure(Main.java:486) at org.mozilla.javascript.tools.shell.Main.processFile(Main.java:452) at org.mozilla.javascript.tools.shell.Main.processSource(Main.java:443) at org.mozilla.javascript.tools.shell.Main.processFiles(Main.java:196) at org.mozilla.javascript.tools.shell.Main$IProxy.run(Main.java:117) at org.mozilla.javascript.Context.call(Context.java:515) at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:507) at org.mozilla.javascript.tools.shell.Main.exec(Main.java:179) at org.mozilla.javascript.tools.shell.Main.main(Main.java:157) Caused by: java.io.FileNotFoundException: foo (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.mozilla.javascript.MemberBox.newInstance(MemberBox.java:194) ... 18 more
If you really want to use exactly the Abort()
function you have above, you can parse the appropriate line out of the stack trace above; alternatively, you can display the whole stack trace, which may be more helpful, depending what you want to do.