java代码
public static native void nativeException();
public static void doException() {
int t = 0 / 0;
}
//调用
try {
JniData.nativeException();
} catch (Exception e) {
e.printStackTrace();
}
native肛码
extern "C"
JNIEXPORT void JNICALL
Java_com_hrg_gys_clfr_myapplication_JniData_nativeException(JNIEnv *env, jclass clazz) {
// 调用java的异常方法
jmethodID jmethodId = env->GetStaticMethodID(clazz, "doException", "()V");
env->CallStaticVoidMethod(clazz, jmethodId); //执行会产生异常 不处理异常应用会崩溃
/*处理异常 应用不会崩溃*/
jthrowable jthrowable1 = env->ExceptionOccurred();
if (jthrowable1) {
env->ExceptionDescribe();
env->ExceptionClear();
/*向java抛出异常*/
jclass jclass1 = env->FindClass("java/lang/Exception");
env->ThrowNew(jclass1, "native exception");
}
}
网友评论