try {
if (typeof hostObj['propertyItDoesntHave'] === "undefined") {
throw new Error('first');
}
} catch (e) {
throw new Error('second');
}
对我来说,这总是崩溃:
org.graalvm.polyglot.PolyglotException: Error: first
at <js>.:anonymous(Unnamed:21) ~[?:?]
at <js>.:=>(Unnamed:21) ~[?:?]
at <js>.:=>(Unnamed:21) ~[?:?]
即从未到达catch块,第一个错误似乎是停止程序。
如何让GraalVM JavaScript解释器捕获这样的嵌套错误?
我认为它应该开箱即用。如果没有,那可能是一个bug。你能把问题提交给吗https://github.com/oracle/graaljs.
它在最简单的测试中确实对我有效,我正在尝试最新版本21.1node
和js
。
which node
~/21.1/graalvm-ce-java11-21.1.0/bin/node
[opc@ol8-demo 21.1]$ node
Welcome to Node.js v14.16.1.
Type ".help" for more information.
> try {
... if (typeof hostObj['propertyItDoesntHave'] === "undefined") {
..... throw new Error('first');
..... }
... } catch (e) {
... throw new Error('second');
... }
Uncaught Error: second
>
>
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
>
[opc@ol8-demo 21.1]$ js
> try {
> if (typeof hostObj['propertyItDoesntHave'] === "undefined") {
> throw new Error('first');
> }
> } catch (e) {
> throw new Error('second');
> }
>
Error: second
at <js> :program(<shell>:1:6:126-144)
>
在Java应用程序中嵌入JS时:
import org.graalvm.polyglot.*;
import org.graalvm.polyglot.Context.Builder;
import org.graalvm.polyglot.proxy.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
try (Context context = Context.newBuilder()
.allowAllAccess(true)
.build()) {
context.eval("js",
"{ " +
"try {" +
" if (typeof hostObj['propertyItDoesntHave'] === "undefined") {" +
" throw new Error('first');" +
" }" +
"} catch (e) {" +
" throw new Error('second'); " +
" } " +
"}");
}
}
}
我确实得到了第二个:
javac Main.java && java Main
Exception in thread "main" Error: second
at <js> :program(Unnamed:1:123-141)
at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:379)
at Main.main(Main.java:12)