On Tue, 30 Jan 2024 13:44:22 GMT, Alexey Ivanov <aiva...@openjdk.org> wrote:
>> In fact, any method can throw an exception… >> >> Is there a guide which clearly states where `ExceptionCheck` is required? >> The function returns a boolean indicating whether there's a pending >> exception. Shall we back out if there is? > >> Is there a guide which clearly states where `ExceptionCheck` is required? > > I should've looked it up. [Java Native Interface > Specification](https://docs.oracle.com/en/java/javase/21/docs/specs/jni/index.html), > section on [handling Java > Exception](https://docs.oracle.com/en/java/javase/21/docs/specs/jni/design.html#java-exceptions) > says: > > <blockquote > cite="https://docs.oracle.com/en/java/javase/21/docs/specs/jni/design.html#exceptions-and-error-codes">Certain > JNI functions use the Java exception mechanism to report error conditions. > In most cases, JNI functions report error conditions by returning an error > code and throwing a Java exception. The error code is usually a special > return value (such as NULL) that is outside of the range of normal return > values. Therefore, the programmer can: > > * quickly check the return value of the last JNI call to determine if an > error has occurred, and > * call a function, `ExceptionOccurred()`, to obtain the exception object that > contains a more detailed description of the error condition. > > There are two cases where the programmer needs to check for exceptions > without being able to first check an error code: > > * The JNI functions that invoke a Java method return the result of the Java > method. The programmer must call `ExceptionOccurred()` to check for possible > exceptions that occurred during the execution of the Java method. > * Some of the JNI array access functions do not return an error code, but may > throw an `ArrayIndexOutOfBoundsException` or `ArrayStoreException`. > > In all other cases, a non-error return value guarantees that no exceptions > have been thrown.</blockquote> > > Thus, `FindClass` returns non-null value if no error occurred; if the > returned value is null, an error occurred, and `FindClass` raised an > exception on the Java side. As per https://docs.oracle.com/en/java/javase/21/docs/specs/jni/design.html#java-exceptions: There are two cases where the programmer needs to check for exceptions without being able to first check an error code: - The JNI functions that invoke a Java method return the result of the Java method. The programmer must call ExceptionOccurred() to check for possible exceptions that occurred during the execution of the Java method. - Some of the JNI array access functions do not return an error code, but may throw an ArrayIndexOutOfBoundsException or ArrayStoreException. In all other cases, a non-error return value guarantees that no exceptions have been thrown. So, the check is needed only after `JNU_CallStaticMethodByName` for `dwmCompositionChanged` (and before other JNI activity) ------------- PR Review Comment: https://git.openjdk.org/jdk/pull/17404#discussion_r1472347241