如何获取 Rhino 中当前的脚本名称和行号?

发布于 2024-11-30 07:39:59 字数 332 浏览 0 评论 0原文

我正在使用 Java 和 Mozilla Rhino 创建一个游戏引擎,我希望所有错误都调用一个函数并为其提供错误消息,例如

...
} 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,但我希望其他人也有同样的事情,例如 IOException。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏有森光若流苏 2024-12-07 07:39:59

这在某种程度上取决于异常是如何抛出的,所以我需要猜测。这取决于您用来执行 Rhino 的优化级别。

我猜测本机 Java 代码会抛出异常(即,您没有使用 throw new Packages.java.io.IOException("...") )。在这种情况下,您可以使用 printStackTrace() 来弄清楚。这是一个小脚本(名为 test.jsh.js,您将在堆栈跟踪中看到它),您可以在 Rhino shell 中运行:

try {
    //  foo does not exist
    var stream = new Packages.java.io.FileInputStream("foo");
} catch (e) {
    e.rhinoException.printStackTrace();
}

... 及其输出:

$ 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

如果您确实想使用上面的 Abort() 函数,您可以从上面的堆栈跟踪中解析相应的行;或者,您可以显示整个堆栈跟踪,这可能会更有帮助,具体取决于您想要执行的操作。

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.

ˉ厌 2024-12-07 07:39:59

为什么不为异常设置某种日志记录功能(例如 Log4j)?这样,您可以设置一个 properties 文件,这样每次您记录某些内容时,它都会以相同的方式输出(而不是总是调用 Abort() 函数。此外,Log4j支持获取行号和其他功能: Log4j模式。

Why don't you setup some kind of logging feature for your exceptions like Log4j? With this, you can set a properties file, that way every time you log something it outputs the same way (instead of always calling an Abort() function. Also, Log4j supports getting line numbers and other features: Log4j Patterns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文