我正在 try catch Android 上 native C++ 代码中的错误。根据文档,当找不到类名时,FindClass 返回 NULL。但我得到了FATAL EXCEPTION: main java.lang.NoClassDefFoundError: fake/class
并且执行永远不会到达 if 语句。
#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;
}
}
此外,此异常会跳过 try-catch block 并使应用程序崩溃。我的Java代码:
static {
System.loadLibrary("native-lib");
}
public native void testError();
...
try {
testError();
} catch (Exception e) {
// exception never get cought
}
我还使用cppFlags '-fexceptions'
。是否有适当的方法来捕获此异常或从中恢复,以便应用程序不会崩溃?
最佳答案
首先,为了防止 native 代码在 FindClass 等上崩溃,您必须在每次 JNI 调用后使用此代码:
bool checkExc(JNIEnv *env) {
if (env->ExceptionCheck()) {
env->ExceptionDescribe(); // writes to logcat
env->ExceptionClear();
return true;
}
return false;
}
实际上,ExceptionClear() 可以阻止崩溃。我找到了这个答案的代码 https://stackoverflow.com/a/33777516/2424777
其次,为了确保捕获 native 代码中的所有崩溃,您必须使用此 try-catch。学分@Richard Critten 也可以在所有 native 函数调用中使用它:
static {
try {
System.loadLibrary("native-lib");
} catch (Error | Exception ignore) { }
}
并且 cppFlags '-fexceptions' 与此无关,因此您可以将其删除。
关于android - 在 Android NDK C++ 代码中捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096235/