GWT: Throw JSNI native exception in Java code

I have some logic in my own method that returns sth or null - they are real and significant states, and I want to throw an exception from the method. Since this is native JSNI, I'm not sure how to do this.

So, consider the 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 an abandoned object?

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

Is there any special type of Gwt exception exception "thrown objects" thrown from JSNI?

+4
source share
2 answers

Regarding Daniel Kurka's answer (and my intuition;)). Then my code might look like this:

 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(); } } } 
+5
source

In the gwt docs:

An exception may be thrown during the execution of any normal Java or JavaScript code as part of the JSNI method. When an exception thrown as part of the JSNI method propagates over the call stack and is caught by the Java catch block, a JavaScript exception is thrown wrapped as a JavaScriptException at the time it is caught. This wrapper object contains only the class name and description for the JavaScript exception. Recommended Practice: Handle JavaScript exceptions in JavaScript code and Java exceptions in Java code.

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

+6
source

All Articles