public final class Exceptions extends Object
ExceptionHandler
and CheckedExceptionHandler
.
Typical usage: Exceptions.log().get(() -> my_throwing_lambda).orElse(fallback)
Usage with checked exceptions: Exceptions.log().run(Exceptions.sneak().runnable(() -> my_throwing_lambda))
Modifier and Type | Method and Description |
---|---|
static ExceptionHandler |
ignore()
Returns ExceptionHandler that lets all exceptions through.
|
static ExceptionHandler |
log()
Returns ExceptionHandler that writes all exceptions to common logger.
|
static ExceptionHandler |
log(Logger logger)
Creates ExceptionHandler that writes all exceptions to the specified logger .
|
static ExceptionHandler |
log(Logger logger, String message)
Creates ExceptionHandler that writes all exceptions to the specified logger with the specified message .
|
static ExceptionHandler |
log(Logger logger, Supplier<String> message)
Creates ExceptionHandler that writes all exceptions to the specified logger with lazily evaluated message .
|
static ExceptionHandler |
pass()
Deprecated.
|
static ExceptionHandler |
silence()
Returns ExceptionHandler that silently ignores all exceptions.
|
static CheckedExceptionHandler |
sneak()
Returns CheckedExceptionHandler that lets through checked exceptions without declaring them.
|
static CheckedExceptionHandler |
wrap()
Returns CheckedExceptionHandler that wraps all checked exceptions.
|
static CheckedExceptionHandler |
wrap(Function<Exception,RuntimeException> wrapper)
Creates CheckedExceptionHandler that applies custom wrapper to checked exceptions.
|
public static ExceptionHandler ignore()
ExceptionHandler
that lets all exceptions through. This exception handler is equivalent to having no exception handler at all. It is useful when switching among several exception handlers at runtime.
@Deprecated public static ExceptionHandler pass()
ExceptionHandler
that lets all exceptions through. This is an old deprecated alias for ignore()
.
public static ExceptionHandler log()
ExceptionHandler
that writes all exceptions to common logger. Logs are written to SLF4J logger named after this class. This handler is convenient and a suitable default choice, but the single shared logger can make logs harder to filter. Use log(Logger)
to specify custom logger where filtering is important.
Typical usage: Exceptions.log().run(() -> my_throwing_lambda)
No exceptions are allowed through, not even Error
s. If InterruptedException
is caught, Thread.interrupt()
is called.
log(Logger)
public static ExceptionHandler log(Logger logger)
ExceptionHandler
that writes all exceptions to the specified logger
. Most application code can use the more convenient log()
method. Use log(Logger, String)
overload to specify unique message where necessary.
Typical usage: Exceptions.log(logger).run(() -> my_throwing_lambda)
No exceptions are allowed through, not even Error
s. If InterruptedException
is caught, Thread.interrupt()
is called.
logger
- where all exceptions are logged
NullPointerException
- if logger
is null
log()
, log(Logger, String)
public static ExceptionHandler log(Logger logger, String message)
ExceptionHandler
that writes all exceptions to the specified logger
with the specified message
. If you just need to specify custom logger, use log(Logger)
. This overload allows for differentiating or explanatory message. If the message is expensive to construct, use log(Logger, Supplier)
method.
Typical usage: Exceptions.log(logger, "Failed to do X.").run(() -> my_throwing_lambda)
No exceptions are allowed through, not even Error
s. If InterruptedException
is caught, Thread.interrupt()
is called.
logger
- where all exceptions are logged
message
- introduces every exception in the log
NullPointerException
- if logger
or message
is null
log(Logger)
, log(Logger, Supplier)
public static ExceptionHandler log(Logger logger, Supplier<String> message)
ExceptionHandler
that writes all exceptions to the specified logger
with lazily evaluated message
. If the message does not need lazy evaluation, use the log(Logger, String)
method. This overload constructs expensive messages lazily only when exception is caught. If message
throws, the exception is logged and fallback message is used to log the original exception.
Typical usage: Exceptions.log(logger, () -> "Exception in " + this).run(() -> my_throwing_lambda)
No exceptions are allowed through, not even Error
s. If InterruptedException
is caught, Thread.interrupt()
is called.
logger
- where all exceptions are logged
message
- a Supplier
that is lazily evaluated to generate log message
NullPointerException
- if logger
or message
is null
log(Logger, String)
public static ExceptionHandler silence()
ExceptionHandler
that silently ignores all exceptions. This handler is useful when some code is known to produce junk exceptions. Most application code should use log()
instead.
Typical usage: Exceptions.silence().run(() -> my_throwing_lambda)
No exceptions are allowed through, not even Error
s. If InterruptedException
is caught, Thread.interrupt()
is called.
log()
public static CheckedExceptionHandler sneak()
CheckedExceptionHandler
that lets through checked exceptions without declaring them. All exceptions are allowed through unmodified, including checked ones, even though no checked exceptions are declared.
Typical usage: Exceptions.sneak().run(() -> my_throwing_lambda)
This is the recommended CheckedExceptionHandler
, because it does not obfuscate the original exception in any way. Use wrap()
if using sneaky exceptions is not possible or not desirable.
Sneaky throw is implemented using a neat trick with type erasure that persuades java compiler to tolerate throwing undeclared checked exception from specially crafted generic method. The trick is safe. It is legal java code and JVMs do not mind it. It is just considered dirty by some developers.
wrap()
public static CheckedExceptionHandler wrap()
CheckedExceptionHandler
that wraps all checked exceptions. Unchecked exceptions are passed through unmodified. Checked exceptions are wrapped in WrappedException
. Use sneak()
to avoid wrapping. Use wrap(Function)
to specify a custom wrapper.
Typical usage: Exceptions.wrap().run(() -> my_throwing_lambda)
If InterruptedException
is caught, Thread.interrupt()
is called.
sneak()
, wrap(Function)
public static CheckedExceptionHandler wrap(Function<Exception,RuntimeException> wrapper)
CheckedExceptionHandler
that applies custom wrapper to checked exceptions. Unchecked exceptions are passed through unmodified. Checked exceptions are passed to wrapper
and the resulting unchecked exception is thrown. Use sneak()
to avoid wrapping and wrap()
to use standard wrapper.
Typical usage: Exceptions.wrap(MyWrapperException::new).run(() -> my_throwing_lambda)
If InterruptedException
is caught, Thread.interrupt()
is called before invoking the wrapper
.
wrapper
- method converting checked exception into an unchecked one, often just exception constructor reference
NullPointerException
- if wrapper
is null
sneak()
, wrap()
Copyright © 2017–2020 Robert Važan. All rights reserved.