Does JNI Throw break the method execution?

Viewed 172

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?

2 Answers

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?

In your particular case the NoClassDefFoundError that was pending right after env->FindClass(name) returned NULL will be overwritten by your hand-throwing exception env->ThrowNew(env->FindClass("java/lang/NoClassDefFoundError"), message) which will be thrown when control is transferred back to Java code.

What you described is an improper way of handling exception in JNI code. You should have checked it with env->ExceptionOccurred(); and then call env->ExceptionClear() to indicate that the exception was handled.

Related