With JNI, you can discard, capture, print, and discard Java exceptions as you would in a Java program. But for programmers, it is necessary to invoke a dedicated JNI function to handle exceptions. Some of the JNI functions used for exception handling are listed below:
Throw (): Discards an existing exception object, which is used in the intrinsic method to discard an exception.
Thrownew (): Generates a new exception object and discards it.
Exceptionoccurred (): Determines if an exception has been discarded, but has not been cleared.
Exceptiondescribe (): Prints an exception and stack trace information.
Exceptionclear (): Clears a pending exception.
FatalError (): Causes a serious error and does not return.
Of all these functions, the most important thing to ignore is exceptionoccurred () and Exceptionclear (). Most JNI functions produce exceptions and do not have the language features that are available as they are in a try block in Java. So after each JNI function call, you must call Exceptionoccurred () to see if the exception has been discarded. If an exception is detected, you can choose to control it (and, if possible, discard it again). However, you must ensure that the exception is eventually cleared. This can be done in your own function using Exceptionclear (), and if the exception is discarded, it may be done in some other function. But in any case, this work is essential.
We must ensure that the anomaly is completely cleared. Otherwise, if you call a JNI function in an exception pending, the results are often unpredictable. There are also a few JNI functions that can be invoked safely in the case of exceptions, but they are all specialized exception control functions.