总结
实验材料:
- Android系统8.1.0 (以下实验结果,可能与Android系统相关相关)
- Java异常(分母为0)
public void makeException() { int i = 1 / 0; }
- Native方法,调用上述Java方法
实验结论
-
Native调用Java异常代码,且Native没有任何异常处理时
- java异常方法中断
- Java异常抛出
- native方法继续运行
- 进程重启
-
Native层调用ExceptionClear
- Java异常不再抛出
- 进程不再重启
-
ExceptionDescribe
- 有Java异常描述打印
Git
tag为exception_v1.0
源代码Git地址
[email protected]:AlbertSnow/GraphicDemo.git
extern "C" JNIEXPORT void JNICALL Java_com_albertsnow_graphicdemo_jni_exception_TestException_testException (
JNIEnv *env, jobject thisObj, jstring msg) {
jclass cls = env->GetObjectClass(thisObj);
jmethodID jmethodId = env->GetMethodID(cls, "makeException", "()V");
LOGE("start method call 1");
env->CallVoidMethod(thisObj, jmethodId);
LOGE("end method call");
if (env->ExceptionCheck()) {
LOGE("Exception occur.");
// env->ExceptionDescribe();
// env->ExceptionClear();
// env->ExceptionOccurred()
}
}
上述源码
2019-10-17 20:10:12.139 26700-26700/com.albertsnow.graphicdemo I/TestException: start method call 1
2019-10-17 20:10:12.139 26700-26700/com.albertsnow.graphicdemo I/TestException: end method call
2019-10-17 20:10:12.139 26700-26700/com.albertsnow.graphicdemo I/TestException: Exception occur.
2019-10-17 20:10:12.139 26700-26700/com.albertsnow.graphicdemo D/AndroidRuntime: Shutting down VM
--------- beginning of crash
2019-10-17 20:10:12.144 26700-26700/com.albertsnow.graphicdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.albertsnow.graphicdemo, PID: 26700
java.lang.ArithmeticException: divide by zero
at com.albertsnow.graphicdemo.jni.exception.TestException.makeException(TestException.java:25)
at com.albertsnow.graphicdemo.jni.exception.TestException.testException(Native Method)
at com.albertsnow.graphicdemo.jni.exception.ExceptionActivity$1.onClick(ExceptionActivity.java:27)
at android.view.View.performClick(View.java:6319)
at android.view.View$PerformClick.run(View.java:24939)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6738)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
2019-10-17 20:10:12.167 26700-26700/? I/Process: Sending signal. PID: 26700 SIG: 9
反注解env->ExceptionDescribe();
2019-10-17 20:09:20.212 26235-26235/com.albertsnow.graphicdemo I/TestException: start method call 1
2019-10-17 20:09:20.213 26235-26235/com.albertsnow.graphicdemo I/TestException: end method call
2019-10-17 20:09:20.213 26235-26235/com.albertsnow.graphicdemo I/TestException: Exception occur.
2019-10-17 20:09:20.214 26235-26235/com.albertsnow.graphicdemo W/System.err: java.lang.ArithmeticException: divide by zero
2019-10-17 20:09:20.215 26235-26235/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.TestException.makeException(TestException.java:25)
2019-10-17 20:09:20.216 26235-26235/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.TestException.testException(Native Method)
2019-10-17 20:09:20.216 26235-26235/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.ExceptionActivity$1.onClick(ExceptionActivity.java:27)
2019-10-17 20:09:20.216 26235-26235/com.albertsnow.graphicdemo W/System.err: at android.view.View.performClick(View.java:6319)
2019-10-17 20:09:20.217 26235-26235/com.albertsnow.graphicdemo W/System.err: at android.view.View$PerformClick.run(View.java:24939)
2019-10-17 20:09:20.217 26235-26235/com.albertsnow.graphicdemo W/System.err: at android.os.Handler.handleCallback(Handler.java:790)
2019-10-17 20:09:20.217 26235-26235/com.albertsnow.graphicdemo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2019-10-17 20:09:20.218 26235-26235/com.albertsnow.graphicdemo W/System.err: at android.os.Looper.loop(Looper.java:192)
2019-10-17 20:09:20.218 26235-26235/com.albertsnow.graphicdemo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6738)
2019-10-17 20:09:20.218 26235-26235/com.albertsnow.graphicdemo W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-10-17 20:09:20.218 26235-26235/com.albertsnow.graphicdemo W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
2019-10-17 20:09:20.219 26235-26235/com.albertsnow.graphicdemo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
2019-10-17 20:09:20.219 26235-26235/com.albertsnow.graphicdemo D/AndroidRuntime: Shutting down VM
2019-10-17 20:09:20.223 26235-26235/com.albertsnow.graphicdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.albertsnow.graphicdemo, PID: 26235
java.lang.ArithmeticException: divide by zero
at com.albertsnow.graphicdemo.jni.exception.TestException.makeException(TestException.java:25)
at com.albertsnow.graphicdemo.jni.exception.TestException.testException(Native Method)
at com.albertsnow.graphicdemo.jni.exception.ExceptionActivity$1.onClick(ExceptionActivity.java:27)
at android.view.View.performClick(View.java:6319)
at android.view.View$PerformClick.run(View.java:24939)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6738)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
2019-10-17 20:09:20.243 26235-26235/com.albertsnow.graphicdemo I/Process: Sending signal. PID: 26235 SIG: 9
反注解env->ExceptionClear();
后VM不再崩溃,和没有异常一样
2019-10-17 20:07:46.101 25759-25759/com.albertsnow.graphicdemo I/TestException: start method call 1
2019-10-17 20:07:46.108 25759-25759/com.albertsnow.graphicdemo I/TestException: end method call
2019-10-17 20:07:46.108 25759-25759/com.albertsnow.graphicdemo I/TestException: Exception occur.
2019-10-17 20:07:46.108 25759-25759/com.albertsnow.graphicdemo W/System.err: java.lang.ArithmeticException: divide by zero
2019-10-17 20:07:46.109 25759-25759/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.TestException.makeException(TestException.java:25)
2019-10-17 20:07:46.109 25759-25759/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.TestException.testException(Native Method)
2019-10-17 20:07:46.110 25759-25759/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.ExceptionActivity$1.onClick(ExceptionActivity.java:27)
2019-10-17 20:07:46.110 25759-25759/com.albertsnow.graphicdemo W/System.err: at android.view.View.performClick(View.java:6319)
2019-10-17 20:07:46.110 25759-25759/com.albertsnow.graphicdemo W/System.err: at android.view.View$PerformClick.run(View.java:24939)
2019-10-17 20:07:46.110 25759-25759/com.albertsnow.graphicdemo W/System.err: at android.os.Handler.handleCallback(Handler.java:790)
2019-10-17 20:07:46.112 25759-25759/com.albertsnow.graphicdemo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2019-10-17 20:07:46.113 25759-25759/com.albertsnow.graphicdemo W/System.err: at android.os.Looper.loop(Looper.java:192)
2019-10-17 20:07:46.113 25759-25759/com.albertsnow.graphicdemo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6738)
2019-10-17 20:07:46.113 25759-25759/com.albertsnow.graphicdemo W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-10-17 20:07:46.114 25759-25759/com.albertsnow.graphicdemo W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
2019-10-17 20:07:46.114 25759-25759/com.albertsnow.graphicdemo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
2019-10-17 20:07:46.114 25759-25759/com.albertsnow.graphicdemo I/ExceptionActivity: receive some inoiooooooooo
主动抛出异常
// 抛出我们自己的异常处理
jclass newExcCls;
newExcCls = env->FindClass("java/lang/Exception");
if (newExcCls == NULL) {
return;
}
env->ThrowNew(newExcCls, "throw from C Code.");
2019-10-17 20:16:13.280 27232-27232/com.albertsnow.graphicdemo I/TestException: start method call 1
2019-10-17 20:16:13.280 27232-27232/com.albertsnow.graphicdemo I/TestException: end method call
2019-10-17 20:16:13.280 27232-27232/com.albertsnow.graphicdemo I/TestException: Exception occur.
2019-10-17 20:16:13.280 27232-27232/com.albertsnow.graphicdemo W/System.err: java.lang.ArithmeticException: divide by zero
2019-10-17 20:16:13.281 27232-27232/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.TestException.makeException(TestException.java:25)
2019-10-17 20:16:13.281 27232-27232/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.TestException.testException(Native Method)
2019-10-17 20:16:13.281 27232-27232/com.albertsnow.graphicdemo W/System.err: at com.albertsnow.graphicdemo.jni.exception.ExceptionActivity$1.onClick(ExceptionActivity.java:27)
2019-10-17 20:16:13.281 27232-27232/com.albertsnow.graphicdemo W/System.err: at android.view.View.performClick(View.java:6319)
2019-10-17 20:16:13.281 27232-27232/com.albertsnow.graphicdemo W/System.err: at android.view.View$PerformClick.run(View.java:24939)
2019-10-17 20:16:13.281 27232-27232/com.albertsnow.graphicdemo W/System.err: at android.os.Handler.handleCallback(Handler.java:790)
2019-10-17 20:16:13.281 27232-27232/com.albertsnow.graphicdemo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2019-10-17 20:16:13.282 27232-27232/com.albertsnow.graphicdemo W/System.err: at android.os.Looper.loop(Looper.java:192)
2019-10-17 20:16:13.282 27232-27232/com.albertsnow.graphicdemo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6738)
2019-10-17 20:16:13.282 27232-27232/com.albertsnow.graphicdemo W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2019-10-17 20:16:13.282 27232-27232/com.albertsnow.graphicdemo W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
2019-10-17 20:16:13.282 27232-27232/com.albertsnow.graphicdemo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
2019-10-17 20:16:13.282 27232-27232/com.albertsnow.graphicdemo D/AndroidRuntime: Shutting down VM
2019-10-17 20:16:13.285 27232-27232/com.albertsnow.graphicdemo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.albertsnow.graphicdemo, PID: 27232
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:560)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
Caused by: java.lang.Exception: throw from C Code.
at com.albertsnow.graphicdemo.jni.exception.TestException.testException(Native Method)
at com.albertsnow.graphicdemo.jni.exception.ExceptionActivity$1.onClick(ExceptionActivity.java:27)
at android.view.View.performClick(View.java:6319)
at android.view.View$PerformClick.run(View.java:24939)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6738)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:550)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:825)
2019-10-17 20:16:13.306 27232-27232/com.albertsnow.graphicdemo I/Process: Sending signal. PID: 27232 SIG: 9