Android NDK Development (6): communication between Java and native code (Exception Handling)

Source: Internet
Author: User

1. Capture exceptions

Exception Handling is a function in Java and is often used in Android development using sdks. If the Android native code encounters an error during execution, it needs to be checked and thrown to the Java layer. An error occurred while executing the native code. For example, if a null pointer or memory leak is used, and no corresponding detection box exception is thrown, the APP will flash back immediately without any prompt.

Exception Handling in JNI is different from that in Java. In Java, the exception is directly captured and then processed accordingly. JNI requires developers to explicitly implement the exception handling flow after an exception occurs. For example:

Public class JavaClass {/*** exception throw Method */private void throwException () throws NullPointerException {throw new NullPointerException ("Null pointer ");} /*** native METHOD */private native void nativeMethod ();}

The native method nativeMethod calls the throwException method. nativeMethod native methods need to be explicitly used for exception handling. JNI provides the ExceptionOccurred function to check whether the VM has an exception. After use, you also need to clear the exception explicitly using the predictionclear function.

jthrowable ex;//...(*env)->CallVoidMethod(env, instance, throwExceptionId);ex = (*env)->ExceptionOccurred(env);if (0 != ex) {    (*env)->ExceptionClear(env);}

Ii. Throw an exception

JNI also allows native code to throw an exception. Because the exception is a Java class, you need to use the FindClass function in JNI to find the exception class, and use the ThrowNew function to initialize and throw a new exception. For example:

jclass clazz;//...clazz = (*env)->FindClass(env, "java/lang/NullPointerException");if (0 != clazz) {    (*env)->ThrowNew(env, clazz, "Exception message.");}

Because native methods are not controlled by virtual machines, the thrown exceptions do not stop the execution of native methods. When an exception is thrown, all allocated resources, such as memory resources... local references obtained through the JNIEnv interface, will be automatically released by the VM after the native method is returned.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.