I have a Java library which run a series of tasks. And I registered a default uncaught exception handler using:
Thread.setDefaultUncaughtExceptionHandler(new JavaUncaughtExceptionHandler());
The UncaughtExceptionHandler implements UncaughtExceptionHandler
and only log the error information in STDERR
.
My Java library is called through JNI from C++ code, and the JNI is calling it with ExceptionCheck()
and log the error as FATAL in C++.
The problem is:
In the runtime, when there's a RuntimeException
(or any other uncaught exception) happens in my Java code, the error got captured in C++ instead of my JavaUncaughtExceptionHandler
registered as thread default uncaught exception handler.
DefaultUncaughtExceptionHandler
actually got called? I know before thread got shutdown, but when specifically in JNI case. Is that called before return to C++ or after C++ code finished as well). I think it's related to the thread management in JNI, please share any related information as well.try
/catch
blocks) Thanks so much.
It sounds like you are talking about the Java Invocation API (i.e., you are calling Java methods from within a C++ program, as opposed to simple JNI where the calls go the other way.)
I'm not a big expert, but I have worked with the Java Invocation API.
As far as I know, an uncaught exception handler only will be invoked when an exception is thrown by a Java thread's run()
method. But there isn't any run()
method in a thread that was created by C/C++ code.
I wrote C, not C++, so my calls into Java all looked like
jobject return_value = (*env)->CallObjectMethod(env, instance, method_id, ...);
When that's called from C, it always returns, but before you use the return_value
, you have to check whether the method returned normally or threw an exception.
If it threw an exception, there's no way for the exception to be "uncaught", If the C code doesn't call (*env)->ExceptionClear(env)
(i.e., if the C code doesn't "catch" the exception), then I don't remember what happens, but I don't think it's good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With