Checking errors and handling exceptions in JNI
JNI functions can fail because of system constraint (for example, lack of memory) or invalid arguments (for example, passing a native UTF-8 string when the function is expecting a UTF-16 string). This recipe discusses how to handle errors and exceptions in JNI programming.
Getting ready
The following recipes should be read first before proceeding with this recipe:
Manipulating strings in JNI
Managing references in JNI
Accessing Java static and instance fields in native code
Calling static and instance methods from native code
How to do it…
Follow these steps to create a sample Android project that illustrates errors and exception handling in JNI:
Create a project named
ExceptionHandling
. Set the package name ascookbook.chapter2
. Create an activity namedExceptionHandlingActivity
. Under the project, create a folder namedjni
. Refer to the Loading native libraries and registering native methods recipe of this chapter for more detailed instructions...