Android NDK Development article: Java and Native code communication (Exception handling)

Source: Internet
Author: User

First, catch the exception

Exception handling is a feature in Java that is often used when developing with the SDK in Android. Android native code will need to detect and throw an exception to the Java layer if it encounters an error during execution. There was a problem with the execution of the native code, such as using a null pointer, a memory leak, and without a corresponding detection box exception thrown, the app will immediately flash back without any hint.

Exception handling in JNI is not the same as in Java. Exception handling in Java is directly captured and then handled accordingly. JNI requires developers to explicitly implement exception-handling flows after an exception occurs. For example, the following example:

  1. Public class Javaclass {
  2. /** 
  3. * Exception Throw method
  4. */
  5. private void ThrowException () throws NullPointerException {
  6. throw New NullPointerException ("Null pointer");
  7. }
  8. /** 
  9. * Native method
  10. */
  11. private native void Nativemethod ();
  12. }

Calling the ThrowException method in native method Nativemethod, Nativemethod native method needs to be displayed to do exception handling. JNI provides the exceptionoccurred function to query whether a virtual machine has a pending exception. After use, the Exceptionclear function is also required to explicitly clear the exception.

    1. Jthrowable ex;
    2. //...
    3. (*env)->callvoidmethod (env, instance, Throwexceptionid);
    4. ex = (*env)->exceptionoccurred (env);
    5. if (0! = ex) {
    6. (*env)->exceptionclear (env);
    7. }

Second, throw an exception

JNI also allows native code to throw exceptions. Since the exception is a Java class, it is necessary to use the Findclass function to find the exception class in Jni, and a new exception can be initialized and thrown with the Thrownew function. For example:

    1. Jclass Clazz;
    2. //...
    3. Clazz = (*env)->findclass (env, "java/lang/nullpointerexception");
    4. if (0! = clazz) {
    5. (*env)->thrownew (env, Clazz, "Exception message.");
    6. }

Because the native method is not controlled by the virtual machine, the exception thrown does not stop the execution of the native method. When throwing an exception, you need to release all the resources that have been allocated, such as memory resources ... Local references obtained through the JNIEnv interface are automatically freed by the virtual machine after the native method returns.

Android NDK Development article: Java and Native code communication (Exception handling)

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.