Catch a Java Exception from C++

h-gen wraps the JNIEnv pointer in a class called IRMIFL::jni_env. Any JNIEnv method that causes a Java exception causes JNIEnv::ExceptionCheck to return true, and causes JNIEnv::ExceptionOccurred to return a jthrowable that represents the thrown exception. IRMIFL::jni_env methods call JNIEnv::ExceptionCheck after calling JNIEnv methods that might throw Java exceptions. If JNIEnv::ExceptionCheck returns true jni_env methods casts the jthrowable to ::java::lang::Throwable * and throws it as a C++ exception. This means that the follow code will catch any Java exceptions that get thrown as a result of JNI calls through jni_env.

try {
    // Do some h-gen calls or direct jni_env calls
    // ...
} catch (::java::lang::Throwable *t) {
    // Handle the exception or call
    // e->throw_object(t);
    // to reset the exception on the java side.
    // ..

You may notice that all the h-gen generated thunk methods use this type of try/catch block to stop the C++ stack unwinding and reset the exception in the JNI layer.

$Id: catch.html 3769 2007-06-08 19:06:43Z hastings $