在Java的编程中,我们经常会遇到各种的异常,也会处理各种的异常。处理异常在java中非常简单,我们通常会使用try-catch-finally来处理,也可以使用throw简单抛出一个异常。那么在jni编程的时候我们又是如何处理异常的呢?
jni规范已经给我们做好了所有需要做的事情。回想一下处理异常的过程:
jni.h中有如下相关的函数的定义:
jint (*Throw)(JNIEnv*, jthrowable); jint (*ThrowNew)(JNIEnv *, jclass, const char *); jthrowable (*ExceptionOccurred)(JNIEnv*); void (*ExceptionDescribe)(JNIEnv*); void (*ExceptionClear)(JNIEnv*); void (*FatalError)(JNIEnv*, const char*);
此外,还有一个函数和他们没放在一起:
jboolean (*ExceptionCheck)(JNIEnv*);
单从名字上我们可以知道用于检测异常的发生的函数有:
根据以上总结,我们写如下功能的测试代码:
native调用java中的方法,java中的方法抛出异常,我们在native中检测异常,检测到后抛出native中的异常,并清理异常。
void native_catchException(JNIEnv *env, jobject obj) { jthrowable exc; jclass cls = (*env)->GetObjectClass(env, obj); jmethodID mid =(*env)->GetMethodID(env, cls, "callbackException", "()V"); if (mid == NULL) { return; } (*env)->CallVoidMethod(env, obj, mid); exc = (*env)->ExceptionOccurred(env); if (exc) { jclass newExcCls; (*env)->ExceptionDescribe(env); (*env)->ExceptionClear(env); newExcCls = (*env)->FindClass(env,"java/lang/IllegalArgumentException"); if (newExcCls == NULL) { /* Unable to find the exception class, give up. */ return; } (*env)->ThrowNew(env, newExcCls, "thrown from C code"); } } static JNINativeMethod gMethods[] = { ... {"exception","()V",(void *)native_catchException}, };
代码的其他部分请参看之前的章节。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.text); try { exception(); } catch (Exception e) { System.out.println("In Java:\n\t" + e); } } private native void exception() throws IllegalArgumentException; private void callbackException() throws NullPointerException { throw new NullPointerException("MainActivity.callbackException"); }
09-26 10:58:50.012 23934-23934/com.jinwei.jnitesthello W/System.err: java.lang.NullPointerException: MainActivity.callbackException 09-26 10:58:50.012 23934-23934/com.jinwei.jnitesthello W/System.err: at com.jinwei.jnitesthello.MainActivity.callbackException(MainActivity.java:24) 09-26 10:58:50.012 23934-23934/com.jinwei.jnitesthello W/System.err: at com.jinwei.jnitesthello.MainActivity.exception(Native Method) 09-26 10:58:50.012 23934-23934/com.jinwei.jnitesthello W/System.err: at com.jinwei.jnitesthello.MainActivity.onCreate(MainActivity.java:32) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.app.Activity.performCreate(Activity.java:6299) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.os.Looper.loop(Looper.java:148) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at java.lang.reflect.Method.invoke(Native Method) 09-26 10:58:50.013 23934-23934/com.jinwei.jnitesthello W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731) 09-26 10:58: