ApplicationExceptionHandler class
A global, thread-safe handler for uncaught exceptions in JetLeaf applications.
The ApplicationExceptionHandler is responsible for intercepting all uncaught exceptions thrown during the execution of the application, either synchronously or asynchronously (within Zones). It ensures that exceptions are:
- Wrapped consistently: Non-Throwable objects (e.g., Exception, Error) are wrapped into RuntimeException for uniform handling.
- De-duplicated: Exceptions are tracked internally to avoid logging the same exception multiple times, including nested causes.
- Delegated: Optionally forwarded to a parent handler provided via the constructor for integration with external logging or error handling frameworks.
- Terminating gracefully: Supports registering a custom exit code that terminates the application when a critical exception occurs.
- Recognized: Known logging-related errors (see
_logConfigurationMessages) are detected and treated specially.
Thread-local storage (_handlerLocal) ensures that each Dart isolate can
maintain its own handler, providing isolated exception handling per execution
context. Use current to access the handler in the current isolate.
Example Usage
void main() {
final handler = ApplicationExceptionHandler.current;
runZonedGuarded(() {
throw Exception("Something went wrong!");
}, handler.uncaughtException);
}
References:
- Throwable: Base class for exceptions in JetLeaf.
- RuntimeException: Wrapper for non-Throwable objects.
- Zone: Dart class for asynchronous exception handling.
_logConfigurationMessages: Recognized log configuration error messages.
Constructors
- ApplicationExceptionHandler([void _parentHandler(Object error, StackTrace stack)?])
- A global, thread-safe handler for uncaught exceptions in JetLeaf applications.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
registerExitCode(
int code) → void - Registers an exit code that will be used when an uncaught exception is encountered.
-
registerLoggedException(
Throwable exception) → void - Registers an exception as already logged to avoid duplicate handling.
-
toString(
) → String -
A string representation of this object.
inherited
-
uncaughtException(
Object exception, StackTrace stackTrace) → void - Handles uncaught exceptions thrown within a Dart Zone.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- current → ApplicationExceptionHandler
-
Returns the ApplicationExceptionHandler associated with the current isolate (thread-local).
no setter