GWT: Catch native JSNI exception in Java code
Asked Answered
M

2

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?

Moorish answered 18/7, 2012 at 17:56 Comment(0)
M
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();
        }
    }
}
Moorish answered 30/8, 2013 at 7:44 Comment(0)
T
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

Tardy answered 18/7, 2012 at 18:18 Comment(4)
you mean com.google.gwt.core.client.JavaScriptException? to be sure :)Moorish
and just one mre question: the mentioned above "description" is retrieved wit getMessage() or toString()?Moorish
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 codeTardy
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 ;).Moorish
M
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();
        }
    }
}
Moorish answered 30/8, 2013 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.