JNI Throw 会破坏方法执行吗?

Does JNI Throw break the method execution?

当我调用env->ThrowNew(clazz, "...")时,C++/C方法的后续执行会停止还是必须自己停止?

// 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;
}

当我调用 load_main_class 方法时会发生什么,它找不到 class 并且调用了 ThrowNew 方法?

JNI 异常 does not immediately disrupt the native method execution。但是如果你没有正确处理这个异常,那么任何 JNI 函数调用(除了极少数明确清除的)都会崩溃。

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?

在您的特定情况下,在 env->FindClass(name) 返回 NULL 之后立即挂起的 NoClassDefFoundError 将被您的手动抛出异常 env->ThrowNew(env->FindClass("java/lang/NoClassDefFoundError"), message) 覆盖,当控制权转移回 Java 代码。

您所描述的是 JNI 代码中处理异常的不正确方式。您应该已经用 env->ExceptionOccurred(); 检查了它,然后调用 env->ExceptionClear() 以表明异常已被处理。