I am trying to catch an error in native C++ code on Android. According to the docs FindClass returns NULL when class name cannot be find. But I got FATAL EXCEPTION: main java.lang.NoClassDefFoundError: fake/class
and execution never reaches the if statement.
#include <string.h>
#include <jni.h>
extern "C"
void Java_com_example_MainActivity_testError(JNIEnv *env, jobject) {
jclass clazz = env->FindClass("fake/class");
// never gets here
if (clazz == NULL) {
return;
}
}
Also this exception skips the try-catch block and crashes the app. My Java code:
static {
System.loadLibrary("native-lib");
}
public native void testError();
...
try {
testError();
} catch (Exception e) {
// exception never get cought
}
Also I use cppFlags '-fexceptions'
. Is there a proper way to catch this exception or recover from it so the app does not crash?