Question

As we know, many methods in JNI may cause exception and cannot be called after exception caused that may crash the JVM. We can not only use ExceptionOccurred() or ExceptionCheck() to determine whether exception occurred or not, but also can simply check something not equal to NULL or 0. Which way is the best practice?

Was it helpful?

Solution

As you are only asking for "best practice", the answer is very straightforward:

  1. always call ExceptionCheck or ExceptionOccurred when calling JNI functions which may raise exceptions.
  2. always check return values from JNI functions

Calling ExceptionOccurred after each JNI call, preferably in a dedicated function, allows you to get some information on the exception, for logging, and also allows you to call ExceptionClear.

Make the dedicated function return void, and use the return values from the JNI functions to distinguish between normal and error paths.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top