@FunctionalInterface public interface ExceptionHandler
ExceptionHandler is a functional interface that provides a mechanism
for handling exceptions in a uniform way throughout an application.
It allows defining custom logic for exception handling depending on different levels and types
of exceptions. This can be particularly useful for logging or taking corrective action based on specific exceptions.
Implementations of this interface can provide custom strategies for handling exceptions, such as logging them, ignoring them, or throwing them.
By default, exceptions are logged using SLF4J logging framework, however, this behavior can be overridden.
| Modifier and Type | Method and Description |
|---|---|
default ExceptionHandler |
defaultHandler()
Retrieves the default underlying exception handler.
|
static ExceptionHandler |
ignoresEverything()
Creates an
ExceptionHandler that ignores all exceptions. |
default boolean |
isEnabled(@NotNull Class<?> aClass)
Checks if the exception handler is enabled for the given class.
|
default void |
on(@NotNull Class<?> clazz,
String message)
Handles an exception occurred in a specific class, with a specific message.
|
default void |
on(@NotNull Class<?> clazz,
@Nullable String message,
@Nullable Throwable thrown)
The default method to call when an exception occurs.
|
default void |
on(@NotNull Class<?> clazz,
Throwable thrown)
Handles an exception occurred in a specific class, with a specific message.
|
default void |
on(@NotNull org.slf4j.Logger logger,
@Nullable String message)
Handles an exception occurred with a specific logger and message.
|
void |
on(@NotNull org.slf4j.Logger logger,
@Nullable String message,
@Nullable Throwable thrown)
Handles an exception occurred with a specific logger, message and throwable.
|
static ExceptionHandler ignoresEverything()
ExceptionHandler that ignores all exceptions.NullExceptionHandler which ignores all exceptions.default void on(@NotNull
@NotNull Class<?> clazz,
Throwable thrown)
clazz - the class where the error occurred. Must not be null.thrown - the throwable instance representing the error.default void on(@NotNull
@NotNull Class<?> clazz,
String message)
clazz - the class where the error occurred. Must not be null.message - a custom message detailing the error.default void on(@NotNull
@NotNull Class<?> clazz,
@Nullable
@Nullable String message,
@Nullable
@Nullable Throwable thrown)
clazz - the class where the exception occurred. Must not be null.message - a custom message providing additional information about the exception, or an empty string if not available.thrown - the exception that needs to be handled, or null if there is no exception.void on(@NotNull
@NotNull org.slf4j.Logger logger,
@Nullable
@Nullable String message,
@Nullable
@Nullable Throwable thrown)
logger - the logger instance to log the error. Must not be null.message - a custom message detailing the error, or null.thrown - the throwable instance representing the error, or null.default void on(@NotNull
@NotNull org.slf4j.Logger logger,
@Nullable
@Nullable String message)
logger - the logger instance to log the error. Must not be null.message - a custom message detailing the error, or null.default boolean isEnabled(@NotNull
@NotNull Class<?> aClass)
aClass - the class to check if the exception handler is enabled for. Must not be null.default ExceptionHandler defaultHandler()
Copyright © 2024. All rights reserved.