Class ExceptionUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Exception>
TlogAndThrow(Supplier<T> exceptionSupplier, String format, Object... args) Logs a formatted error message and then throws a new instance of a specified exception.static <T extends Exception>
voidlogAndThrowWithClass(Class<T> exceptionClass, String format, Object... args) Logs an error message and then securely throws a new instance of the specified exception class.static <T extends Exception>
voidlogAndThrowWithMessage(Function<String, T> exceptionFunction, String format, Object... args) Utility method to safely log an error message and subsequently throw a new exception instance containing that same formatted message.
-
Method Details
-
logAndThrow
public static <T extends Exception> T logAndThrow(Supplier<T> exceptionSupplier, String format, Object... args) throws T Logs a formatted error message and then throws a new instance of a specified exception. This method is a safe and generic way to handle exceptions by using aSupplier, which avoids the complexities and potential runtime errors of Reflection. It leverages the logging framework's ability to format messages, which is more efficient because the message is only constructed if the log level is active.- Type Parameters:
T- The type of the exception to be thrown. This must be a subclass ofException.- Parameters:
exceptionSupplier- ASupplierthat provides an instance of the exception to be thrown. The supplier'sget()method should create and return a new exception instance, typically via a lambda or a constructor reference (e.g.,IllegalArgumentException::new).format- A parameterized message format string compatible with the logging framework's formatters (e.g., using `{}`). This format string will be logged to the error level.args- A variable-length argument list of objects to be substituted into the format string. These objects correspond to the `{}` placeholders in theformatstring.- Returns:
- The exception instance provided by the supplier.
- Throws:
T- The exception instance provided by theexceptionSupplier.
-
logAndThrowWithMessage
public static <T extends Exception> void logAndThrowWithMessage(Function<String, T> exceptionFunction, String format, Object... args) throws TUtility method to safely log an error message and subsequently throw a new exception instance containing that same formatted message.This ensures that the exception thrown, even if generic (like RuntimeException), always contains the context provided in the 'format' string, which is crucial for troubleshooting in environments that might discard the original cause's details.
- Type Parameters:
T- The type of the Exception to be thrown, which must extend Exception.- Parameters:
exceptionFunction- A function that accepts the final formatted message (String) and returns a new exception of type T (e.g.,message -> new MyCustomException(message)).format- The format string for the error message (compatible with String.format).args- Arguments referenced by the format specifiers in the format string.- Throws:
T- The exception created by the provided function.
-
logAndThrowWithClass
public static <T extends Exception> void logAndThrowWithClass(Class<T> exceptionClass, String format, Object... args) throws T Logs an error message and then securely throws a new instance of the specified exception class.This method uses Java Reflection to reliably instantiate the exception by explicitly calling the constructor that accepts a single
Stringargument (the message). This approach avoids the 'Ambiguous method overloading' errors often encountered in Groovy environments when using dynamic closures or constructors withnullparameters.The method ensures that the thrown exception always contains the detailed, formatted error message, preventing common 'No message' issues in the calling environment.
- Type Parameters:
T- The type of Exception to be thrown, which must extendException.- Parameters:
exceptionClass- TheClassobject of the exception to instantiate (e.g.,IllegalArgumentException.class).format- The format string for the error message, compliant withString.format(String, Object...).args- The arguments referenced by the format specifiers in the format string.- Throws:
T- The instantiated and thrown exception of type T, containing the formatted message.RuntimeException- if the specified exception class does not have a public constructor that accepts a single String argument, or if instantiation fails due to reflection errors.
-