#2717 Java Exceptions catching in fantom

Andrey Zakharov Tue 6 Nov

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

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

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

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

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

SlimerDude Thu 8 Nov

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

Andrey Zakharov Thu 8 Nov

Interop.toJava(e) made the trick Thanks!

Login or Signup to reply.