Android Ndk 学习笔记(目录)
public native void exception1();
public native void exception2() throws NoSuchFieldException;
public native void exception3();
exception1();
try {
exception2();
} catch (NoSuchFieldException exception) {
exception.printStackTrace();
Log.d("Derry", "exceptionAction: 异常被我捕获了");
}
exception3();
extern "C"
JNIEXPORT void JNICALL
Java_com_cn_mynativestudy_MainActivity2_exception1(JNIEnv *env, jobject thiz) {
jclass j_class = env->GetObjectClass(thiz);
jfieldID f_id = env->GetFieldID(j_class,"named", "Ljava/lang/String;");
jthrowable thr = env->ExceptionOccurred();
if(thr){
LOGI("C++ 有异常 ,监测到了 ");
env->ExceptionClear();
jfieldID f_id = env->GetFieldID(j_class,"name", "Ljava/lang/String;");
}
}
extern "C"
JNIEXPORT void JNICALL
Java_com_cn_mynativestudy_MainActivity2_exception2(JNIEnv *env, jobject thiz) {
jclass j_class = env->GetObjectClass(thiz);
jfieldID f_id = env->GetFieldID(j_class,"named", "Ljava/lang/String;");
jthrowable thr = env->ExceptionOccurred();
jthrowable jthrowable = env->ExceptionOccurred();
if(jthrowable) {
LOGD("C++层有异常 监测到了");
env->ExceptionClear();
jclass clz = env->FindClass("java/lang/NoSuchFieldException");
env->ThrowNew(clz, "NoSuchFieldException 是在是找不到 name8888啊,没有办法,抛给你了");
}
}
extern "C"
JNIEXPORT void JNICALL
Java_com_cn_mynativestudy_MainActivity2_exception3(JNIEnv *env, jobject thiz) {
jclass j_class = env->GetObjectClass(thiz);
jmethodID showID = env->GetStaticMethodID(j_class, "show", "()V");
env->CallStaticVoidMethod(j_class, showID);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
}
}