Class ExceptionHandler
Handles exceptions thrown by Java callback methods.
When a Java callback method that is invoked from native code throws an exception, the JVM will immediately crash. Java-GI therefore catches these exceptions.
A global handler that will be invoked for these exceptions can be set with
setUncaughtExceptionHandler(ExceptionHandler.UncaughtExceptionHandler). The handler is not bound to a specific
thread; it will be run for all exceptions from Java callbacks invoked from
native code.
When no handler was set, the exception is instead stored in a ThreadLocal
field and will later be thrown (wrapped in a
CallbackInvocationException), immediately after a native method call in
the same thread has completed.
Exception handling can optionally be disabled by setting the environment
variable java-gi.discard-callback-exceptions to "true" (ignoring case).
When the environment variable java-gi.log-callback-exceptions is set to
"true" (ignoring case), Java-GI will log the exception on stderr (using
g_log() with level WARNING).
For performance reasons, the values of the two environment variables are cached, so the log/rethrow behavior cannot be changed during runtime.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for handlers invoked when a runtime exception is thrown from a Java callback called from native code. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable ExceptionHandler.UncaughtExceptionHandlerGet the handler that was previously set withsetUncaughtExceptionHandler(ExceptionHandler.UncaughtExceptionHandler).static voidhandleException(Throwable throwable, String source) Handles exceptions that were thrown in a Java callback method that was invoked from native code.static voidWhen propagation of exceptions is not disabled with the envrionment variablejava-gi.discard-callback-exceptions, and an exception was stored withhandleException(Throwable, String), the exception is wrapped in aCallbackInvocationExceptionand thrown.static voidsetUncaughtExceptionHandler(@Nullable ExceptionHandler.UncaughtExceptionHandler handler) Set a handler to be invoked when a runtime exception occurs from Java callbacks called from native code.
-
Constructor Details
-
ExceptionHandler
public ExceptionHandler()
-
-
Method Details
-
getUncaughtExceptionHandler
Get the handler that was previously set withsetUncaughtExceptionHandler(ExceptionHandler.UncaughtExceptionHandler). When no handler was set, this will returnnull.- Returns:
- the handler, or
null
-
setUncaughtExceptionHandler
public static void setUncaughtExceptionHandler(@Nullable ExceptionHandler.UncaughtExceptionHandler handler) Set a handler to be invoked when a runtime exception occurs from Java callbacks called from native code. This will replace the normal handling by Java-GI, where the exception is temporarily stored and later rethrown after a native function call has completed. Pass anullto remove a previously set handler.- Parameters:
handler- the exception handler. Ifnull, any previously set handler will be unset.
-
handleException
Handles exceptions that were thrown in a Java callback method that was invoked from native code.
When the environment variable
java-gi.log-callback-exceptionsis"true"(ignoring casse), the exception will be logged tostderr.When the environment variable
java-gi.discard-callback-exceptionsis"true"(ignoring case), the exception is discarded.If a handler has been set with
setUncaughtExceptionHandler(ExceptionHandler.UncaughtExceptionHandler)the handler is invoked to handle the exception. Otherwise, the exception is stored in a ThreadLocal field to be handled later.- Parameters:
throwable- the exception to handlesource- the location where the exception occured
-
propagateExceptions
When propagation of exceptions is not disabled with the envrionment variable
java-gi.discard-callback-exceptions, and an exception was stored withhandleException(Throwable, String), the exception is wrapped in aCallbackInvocationExceptionand thrown.Note: The cause of the exception points to the actual location where the exception occured, while the outer CallbackInvocationException is thrown by the first method that completed in the same thread where the original exception occured. For example, when a GLib idle callback throws an exception, it will be rethrown from a completely unrelated callsite that just happened to be scheduled after the idle callback on the GLib main loop.
- Throws:
CallbackInvocationException- wraps the exception that occured in a Java callback method.
-