JNI - Exception Handling
来源:互联网 发布:centos yum nmap 编辑:程序博客网 时间:2024/05/29 19:18
.java file:
class CatchThrow{private native void doit()throws IllegalArgumentException;private void callback() throws NullPointerException{throw new NullPointerException("CathThrow.callback");}public static void main(String args[]){CatchThrow c = new CatchThrow();try {c.doit();} catch (Exception e){System.out.println("In Java:\n\t" + e);}}static {System.loadLibrary("CatchThrow");}}
.c file:
#include <jni.h>#include <stdio.h>#include "CatchThrow.h"JNIEXPORT void JNICALL <span style="color:#000099;">Java_CatchThrow_doit //do it.</span> (JNIEnv *env, jobject obj){jthrowable exc;jclass cls = (*env)->GetObjectClass(env,obj);jmethodID mid = (*env)->GetMethodID(env, cls, "callback", "()V");if (mid == NULL) return;<span style="color:#000099;">(*env)->CallVoidMethod(env, obj, mid);//Invoke callback</span>exc = (*env)->ExceptionOccurred(env);if (exc){jclass newExcCls;<span style="color:#009900;">(*env)->ExceptionDescribe(env);//This stmt describes the stack trace.</span>(*env)->ExceptionClear(env);newExcCls = (*env)->FindClass(env, "java/lang/IllegalArgumentException");if (newExcCls == NULL){return;}(*env)->ThrowNew(env, newExcCls, "thrown from C code");}}
Result:
Exception in thread "main" java.lang.NullPointerException: CathThrow.callbackat CatchThrow.callback(CatchThrow.java:5)at CatchThrow.doit(Native Method)at CatchThrow.main(CatchThrow.java:10)In Java:java.lang.IllegalArgumentException: thrown from C code
JNI need to explicitly implement the control flow after an exception has occurred.
0 0
- JNI - Exception Handling
- Exception handling
- Exception Handling
- Exception Handling
- Exception Handling
- Exception Handling
- Exception Handling
- exception handling
- Exception Handling
- C# Exception Handling
- Exception Handling Rules
- Structured Exception Handling(SEH)
- Exception Handling in C#
- Vectored Exception Handling
- EAI Exception Handling Patterns
- Java exception handling
- Exception Handling in C#
- Structured Exception Handling
- C#集合类
- WMI技术介绍和应用——WMI概述
- Unity AnimatorController注意事项
- BeagleBoneBlack学习之串口通信
- vs和xmarin都安装完成了,布局没有智能提示的解决办法
- JNI - Exception Handling
- 用Matlab把散点拟合成曲面图
- mysql order by内排序
- 配置和使用VS2013 MFC的若干问题
- ERROR:Simulator:861 - Failed to link the design
- 58同城高性能移动PUSH推送平台架构演进之路
- 归并排序
- [剑指offer]替换空格
- CAS :compare and swap非阻塞同步算法