Java – how do I get the current script name and line number in rhino?

I am using Java and Mozilla rhino to create a game engine. I want all errors to call a function and provide it with error messages, such as

...
} 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 provide the same thing for others, such as IOException

Solution

It depends on how the exception is thrown, so I need to guess It depends on the optimization level you use to execute rhino

I guess the exception was thrown from the native java code (that is, you didn't use throw new packages.java.io.ioexception ("...")) In this case, you can use printstacktrace () to solve it This is a small script (named test.jsh.js, which you will see in the stack trace) that 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.Member@R_961_2419@.newInstance(Member@R_961_2419@.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.Member@R_961_2419@.newInstance(Member@R_961_2419@.java:194)
        ... 18 more

If you really want to use the above abort() function, you can parse the corresponding line in the above stack trace; Alternatively, you can display the entire stack trace, which may be more helpful, depending on what you want to do

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>