Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Exceptions

This Android sample shows how to handle exceptions across the JNI boundary.

Handling native exceptions

Native exceptions can be caught in JNI methods and re-thrown in the JVM. Because uncaught native exceptions will cause your app to crash, we recommend catching all exceptions as a fail-safe. You may also want to catch instances of std::exception or your own exception interface:

extern "C" JNIEXPORT void JNICALL
Java_com_example_exceptions_MainActivity_throwsException(JNIEnv* env,
                                                         jobject /* this */) {
  try {
    might_throw();
  } catch (std::exception& e) {
    jniThrowRuntimeException(env, e.what());
  } catch (...) {
    jniThrowRuntimeException(env, "Catch-all");
  }
}

Then, you can do the same in your Java/Kotlin code:

    try {
        jniMethodThatMightThrow();
    } catch (e: java.lang.RuntimeException) {
        // Handle exception
    }

The hard part here is using the JNI API to throw an exception in the JVM. That is, implementing jniThrowRuntimeException. We recommend referring to JNIHelp.h and JNIHelp.c in the Android platform's libnativehelper, from which exception_helper.h and exception_helper.cpp are adapted.

Screenshot

screenshot