gpt4 book ai didi

java - GWT:在 Java 代码中捕获 native JSNI 异常

转载 作者:搜寻专家 更新时间:2023-11-01 01:22:51 25 4
gpt4 key购买 nike

我在 native 方法中有一些逻辑,它返回 sth 或 null - 它们都是有效且有意义的状态,我想在方法失败时抛出异常。由于它是原生 JSNI,我不确定该怎么做。

于是考虑方法:

public final native <T> T myNativeMethod() /*-{

//..some code


//in javascript you can throw anything, not only the exception object:
throw "something";

}-*/;

但是如何接住抛出的物体呢?

void test() {
try {
myNativeMethod();
}
catch(Throwable e) { // what to catch here???
}
}

是否有任何特殊的 Gwt 异常类型包装从 JSNI 抛出的“异常对象”?

最佳答案

来自 gwt 文档:

An exception can be thrown during the execution of either normal Java code or the JavaScript code within a JSNI method. When an exception generated within a JSNI method propagates up the call stack and is caught by a Java catch block, the thrown JavaScript exception is wrapped as a JavaScriptException object at the time it is caught. This wrapper object contains only the class name and description of the JavaScript exception that occurred. The recommended practice is to handle JavaScript exceptions in JavaScript code and Java exceptions in Java code.

这是完整的引用资料: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#exceptions

关于java - GWT:在 Java 代码中捕获 native JSNI 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11547460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com