5

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. As it is native JSNI i am not sure how to do that.

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?

2 Answers 2

6

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();
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

6

From the gwt docs:

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.

Here is the complete reference: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#exceptions

4 Comments

you mean com.google.gwt.core.client.JavaScriptException? to be sure :)
and just one mre question: the mentioned above "description" is retrieved wit getMessage() or toString()?
basically handle your JavaScript Exceptions in JavaScript and the Java Exceptions in Java. If you really need to throw something you can catch JavaScriptException in your Java code
i know there is a suggestion, in doc you put above, to handle js exceptions in jsni, but in this case i'd like to intentionally ignore it ;).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.