#2717 Java Exceptions catching in fantom

Andrey Zakharov Tue 6 Nov 2018

Hello, all!

I've java calls, that throws exception with valuable information within fields of this exception instance. But in fantom layer I could not catch nothing except Err classes, and this Java Exception obviously not a one of it.

Is there any way to make it works?

// Fantom
try {
 javaFunc() // throws 
} catch ( ComplexException ex ) {
 print ( ex.field )
}



// java side
void javaFunc() {

 // oh! something wrong, throw ex with info
 throw new ComplexException( [ 0, 1, 2 ] );

}

brian Wed 7 Nov 2018

I think that case you have to catch a generic Err and then cast it to your Java type. When Java FFI was designed Err didn't actually subclass from java.lang.Throwable. Now that we fixed that I can probably fix Java FFI to allow catching Java types directly. But that doesn't work today

Andrey Zakharov Thu 8 Nov 2018

catch a generic Err and then cast it to your Java type.

Inconvertible types sys::Err and [java]sedona::Schema$MissingKitManifestException

and e.cause is null...

looks like there is no way to retrieve java exception obj in Fantom

brian Thu 8 Nov 2018

That sounds like a bug, try something like this to by pass compiler:

err := (YourException) ((Obj?)err)

SlimerDude Thu 8 Nov 2018

Try using the Interop class as per FFI: How to dal with java exceptions

Andrey Zakharov Thu 8 Nov 2018

Interop.toJava(e) made the trick Thanks!

jhughes Thu 3 Jan

I have a custom java exception which extends java.lang.Exception which then in turn gets subclassed. If I run Interop.toJava on those subclasses, it reports it's type as

java.lang.Exception:<sublass path>: <error message>

Detecting the typeOf of the resulting interop method returns java.lang::Exception which loses all concept of the actual underlying custom java exception classing.

So what I have now is a custom exception that with java i could easily run a instanceof and detect that any of the exceptions that are extended from this and handle them accordingly. With fantom I can only detect this underlying java class with the interop command but once I do that, it doesn't seem to care about subclassing and only gives me info about the base exception class.

So how do you deal with detecting java exceptions that are sub classed?

Also, even though I know the exception I am getting back, I can't even cast it to the class I know it is which makes me think the interop method is causing the issue on losing the class information.

brian Fri 4 Jan

I don't follow what exactly you are trying to do - use a catch, or is operator or something like that? Maybe provide a simple snippet of what you would like to do, what you are trying to do, and what is failing (use exceptions from std java library we use as a test case)

jhughes Fri 4 Jan

In an attempt to write some code to show it more clearly, I found my test code worked as I had originally expected. Looked further up the chain in my real code and the exception was being incorrectly handled and wrapped then rethrown incorrectly. Once i resolved that the Interop.toJava method returned the java exceptions as expected.

SlimerDude Fri 4 Jan

Hi Jonathan, no worries - I often find the same thing; I solve the problem when trying to reduce it down a simple, reproducible code snippet.

In fact, as much as it can be a pain to so, I usually force myself to do so as a matter of course in debugging as it often gives a win, win... if stuff starts working, win! If not, I've still got a concise code example that not only demonstrates the problem, but also helps me (and others) work out what's going on.

Login or Signup to reply.