tweag/inline-java

Don't write on stderr each time an Exception occurs

nbacquey opened this issue · 2 comments

Currently, each time an Exception occurs on the Java side, it is caught in JNI.Unsafe.Internal, printed on stderr, and tranformed into a JVMException on the Haskell side, where it is thrown with throwIO.

It's problematic because this printing on stderr occurs even if the exception is caught afterwards, and may clutter stdout, particularly on test cases, where multiple exceptions may be expected.

Instead, we could capture the message of the Java Exception, and include it in the Show instance for JVMException. This would allow us to print it only when needed

I would add that moreover, currently printing the JVMException is rather useless as it looks like JVMException (0xsomepointer) which explains little about the cause.

Storing the message in the JVMException type could be too expensive since JNI calls are necessary to retrieve the message.

One way to go about it is to do these jni calls when calling show on the exception (via unsafePerformIO). This would work as long as the JVM isn't shutdown, which is another thing to consider, and it would require multiple JNI calls to get the necessary text.

Another solution would be to offer a function that prints the exception to stderr by calling e.printStackTrace(). I think, I would prefer this for now.

Closed by #168