Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JNI Throw break the method execution?

When I call env->ThrowNew(clazz, "..."), will the subsequent execution of the C++/C method be stopped or do I have to stop it myself?

// This is the method which checks if a class can be loaded.

static inline jclass _check_find_class(JNIEnv* env, char* name) {
    jclass clazz = env->FindClass(name);

    if (clazz == NULL) {
        env->ThrowNew(env->FindClass("java/lang/NoClassDefFoundError"), message);
    }

    return clazz;
}

// This method is called in other functions like

jclass load_main_class(JNIEnv* env) {
    auto clazz = _check_find_class(env, "class/which/cannot/be/loaded");

    do_stuff(env, clazz);

    return clazz;
}

What is going to happen when I call load_main_class method, it isn't able to find the class and the ThrowNew method is called?

like image 945
sb27 Avatar asked Mar 04 '23 07:03

sb27


1 Answers

A JNI exception does not immediately disrupt the native method execution. But if you don't handle this exception correctly, then any JNI function call (except the very few ones that are explicitly cleared) will crash.

like image 167
Alex Cohn Avatar answered Mar 12 '23 16:03

Alex Cohn