JNI exception catching and handling

  • Case 1:
  • Case 2
  • Is three

JNI exception capture and handling, common 3 cases:

  • Native layer code fails and native consumes error messages
  • Java layer code error, resulting in native layer call error
  • Native layer code problem, calling Java layer code error

Case 1:

Native layer code fails and native consumes error messages

Native layer code:

extern "C"
JNIEXPORT void JNICALL
Java_com_example_jni_activity_ExceptionActivity_nativeException1(JNIEnv *env, jobject thiz) {

    jclass j_class = env->GetObjectClass(thiz);

    // This line of code will report an error!!
    jfieldID j_id = env->GetFieldID(j_class, "name"."Ljava/lang/String;");
    / / TODO a way
    // Determine whether an exception has occurred
    jthrowable j_thr = env->ExceptionOccurred();
    if (j_thr) {
        LOGE("Exception detected");
        // Clear the exceptionenv->ExceptionClear(); }}Copy the code

Auxiliary graph:



If the name attribute is not found now,name is reported as an error.

env->ExceptionOccurred();
Copy the code

JNI code can be detected if the error is reported, then clear the error:

 env->ExceptionClear();
Copy the code

Case 2

Java layer code error, resulting in native layer call error

Native layer code:

extern "C"
JNIEXPORT void JNICALL
Java_com_example_jni_activity_ExceptionActivity_nativeException2(JNIEnv *env, jobject thiz) {
    jclass j_class = env->GetObjectClass(thiz);
    // This line of code will report an error!!
    jfieldID j_id = env->GetFieldID(j_class, "name"."Ljava/lang/String;");

    / / TODO a way
    // Determine whether an exception has occurred
    jthrowable j_thr = env->ExceptionOccurred();
    // Non-zero true
    if (j_thr) {
        LOGE("Exception detected");
        // Clear the exception
        env->ExceptionClear();

        // The parameter is the exception package name path
        jclass null_class = env->FindClass("java/lang/NullPointerException");

        env->ThrowNew(null_class, "NullPointerException!!"); }}Copy the code

Auxiliary graph:



If the Java layer code throws a null pointer exception when calling the Java layer code, the JNI layer must handle it accordingly

Similar to case 1, determine whether there is an exception, if there is an exception and then clear the exception, and then return the exception information to the Java layer

// The parameter is the exception package name path
jclass null_class = env->FindClass("java/lang/NullPointerException");

env->ThrowNew(null_class, "NullPointerException!!");
Copy the code

FindClass () :

  • Parameter 1: Enter the path of the error message:

The running results are as follows:

Is three

Native layer code problem, calling Java layer code error

Native layer code:

/ / TODO three ways
extern "C"
JNIEXPORT void JNICALL
Java_com_example_jni_activity_ExceptionActivity_nativeException3(JNIEnv *env, jobject thiz) {
    jclass j_class = env->GetObjectClass(thiz);

    jmethodID j_id = env->GetStaticMethodID(j_class, "showExpetcion"."()V");
    // Call the exception method of the Java layer
    env->CallStaticVoidMethod(j_class, j_id);

    // Check to see if it crashes.
    if (env->ExceptionCheck()) {
        // Description error information is displayed
        env->ExceptionDescribe();
        // Clear the crash informationenv->ExceptionClear(); }}Copy the code

Auxiliary graph:



You can mask the Java layer error exception message

The complete code

Other JNI articles:

Introduction to Android JNI

Android JNI :Android JNI

Android JNI QQ funny voice combat (including complete Demo)

Andoird JNI Dynamic Registration with JNI threads

Original is not easy, your praise is my biggest support ~