简体   繁体   English

GWT:在Java代码中捕获本机JSNI异常

[英]GWT: Catch native JSNI exception in Java code

I have some logic in native method, which returns sth or null - they are both valid and meaningful states, and I want to throw an exception on method's failure. 我在native方法中有一些逻辑,它返回sth或null - 它们都是有效和有意义的状态,我想在方法失败时引发异常。 As it is native JSNI i am not sure how to do that. 由于它是原生JSNI,我不知道该怎么做。

So consider method: 所以考虑方法:

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

    //..some code


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

}-*/;

but how to catch the thrown object? 但如何抓住抛出的物体?

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

Is there any special Gwt Exception Type wrapping "exception objects" thrown from JSNI? 是否有任何特殊的Gwt异常类型包装从JSNI抛出的“异常对象”?

From the gwt docs: 来自gwt文档:

An exception can be thrown during the execution of either normal Java code or the JavaScript code within a JSNI method. 在执行普通Java代码或JSNI方法中的JavaScript代码期间,可能会抛出异常。 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. 当JSNI方法中生成的异常向上传播调用堆栈并被Java catch块捕获时,抛出的JavaScript异常在捕获时被包装为JavaScriptException对象。 This wrapper object contains only the class name and description of the JavaScript exception that occurred. 此包装器对象仅包含发生的JavaScript异常的类名和描述。 The recommended practice is to handle JavaScript exceptions in JavaScript code and Java exceptions in Java code. 建议的做法是处理JavaScript代码中的JavaScript异常和Java代码中的Java异常。

Here is the complete reference: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#exceptions 以下是完整的参考资料: http//www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#exceptions

As to Daniel Kurka's answer (and my intuition ;)). 至于丹尼尔库尔卡的答案(和我的直觉;))。 My code could then look like that: 我的代码可能看起来像那样:

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

    //..some code


    //in javascript you can throw anything it not just only exception object:
    throw "something"; 

    //or in another place of code
    throw "something else";

    //or:
    throw new (function WTF() {})();

}-*/;

void test() throws SomethingHappenedException, SomethingElseHappenedException, UnknownError {
    try {
        myNativeMethod();
    }
    catch(JavaScriptException e) { // what to catch here???

        final String name = e.getName(), description = e.toString(); 

        if(name.equalsIgnoreCase("string")) {

            if(description.equals("something")) {
                throw new SomethingHappenedException(); 
            }
            else if(description.equals("something else")) {
                throw new SomethingElseHappenedException(); 
            }
        }
        else if(name.equals("WTF")) {
            throw new UnknownError();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM