- java.lang.Object
-
- com.machinezoo.noexception.Exceptions
-
public final class Exceptions extends Object
Static methods for creating predefined exception handlers. Custom exception handlers can be created by inheriting fromExceptionHandlerandCheckedExceptionHandler.Typical usage:
Exceptions.silence().get(() -> my_throwing_lambda).orElse(fallback)Usage with checked exceptions:
Exceptions.silence().run(Exceptions.sneak().runnable(() -> my_throwing_lambda))- See Also:
- NoException tutorial
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ExceptionHandlerignore()Deprecated.Usepropagate()instead.static ExceptionHandlerlog()Deprecated.Use SLF4J extension instead.static ExceptionHandlerlog(org.slf4j.Logger logger)Deprecated.Use SLF4J extension instead.static ExceptionHandlerlog(org.slf4j.Logger logger, String message)Deprecated.Use SLF4J extension instead.static ExceptionHandlerlog(org.slf4j.Logger logger, Supplier<String> message)Deprecated.Use SLF4J extension instead.static ExceptionHandlerpass()Deprecated.Usepropagate()instead.static ExceptionHandlerpropagate()ReturnsExceptionHandlerthat propagates (lets through) all exceptions.static ExceptionHandlersilence()ReturnsExceptionHandlerthat silently ignores all exceptions.static CheckedExceptionHandlersneak()ReturnsCheckedExceptionHandlerthat lets through checked exceptions without declaring them.static CheckedExceptionHandlerwrap()ReturnsCheckedExceptionHandlerthat wraps all checked exceptions.static CheckedExceptionHandlerwrap(Function<Exception,RuntimeException> wrapper)CreatesCheckedExceptionHandlerthat applies custom wrapper to checked exceptions.
-
-
-
Method Detail
-
propagate
public static ExceptionHandler propagate()
ReturnsExceptionHandlerthat propagates (lets through) all exceptions. This exception handler is equivalent to having no exception handler at all. It is useful when switching among several exception handlers at runtime.- Returns:
- propagating exception handler
-
ignore
@Deprecated public static ExceptionHandler ignore()
Deprecated.Usepropagate()instead.ReturnsExceptionHandlerthat lets all exceptions through. This is an old deprecated alias forpropagate().- Returns:
- pass-through exception handler
-
pass
@Deprecated public static ExceptionHandler pass()
Deprecated.Usepropagate()instead.ReturnsExceptionHandlerthat lets all exceptions through. This is an old deprecated alias forpropagate().- Returns:
- pass-through exception handler
-
log
@Deprecated public static ExceptionHandler log()
Deprecated.Use SLF4J extension instead.ReturnsExceptionHandlerthat 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. Uselog(Logger)to specify custom logger where filtering is important.Typical usage:
Exceptions.log().run(() -> my_throwing_lambda)No exceptions are allowed through, not even
Errors. IfInterruptedExceptionis caught,Thread.interrupt()is called.- Returns:
- logging exception handler
- See Also:
-
log(Logger)
-
log
@Deprecated public static ExceptionHandler log(org.slf4j.Logger logger)
Deprecated.Use SLF4J extension instead.CreatesExceptionHandlerthat writes all exceptions to the specifiedlogger. Most application code can use the more convenientlog()method. Uselog(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
Errors. IfInterruptedExceptionis caught,Thread.interrupt()is called.- Parameters:
-
logger- where all exceptions are logged - Returns:
- exception handler with custom logger
- Throws:
-
NullPointerException- ifloggerisnull - See Also:
-
log(),log(Logger, String)
-
log
@Deprecated public static ExceptionHandler log(org.slf4j.Logger logger, String message)
Deprecated.Use SLF4J extension instead.CreatesExceptionHandlerthat writes all exceptions to the specifiedloggerwith the specifiedmessage. If you just need to specify custom logger, uselog(Logger). This overload allows for differentiating or explanatory message. If the message is expensive to construct, uselog(Logger, Supplier)method.Typical usage:
Exceptions.log(logger, "Failed to do X.").run(() -> my_throwing_lambda)No exceptions are allowed through, not even
Errors. IfInterruptedExceptionis caught,Thread.interrupt()is called.- Parameters:
-
logger- where all exceptions are logged -
message- introduces every exception in the log - Returns:
- exception handler with custom logger and message
- Throws:
-
NullPointerException- ifloggerormessageisnull - See Also:
-
log(Logger),log(Logger, Supplier)
-
log
@Deprecated public static ExceptionHandler log(org.slf4j.Logger logger, Supplier<String> message)
Deprecated.Use SLF4J extension instead.CreatesExceptionHandlerthat writes all exceptions to the specifiedloggerwith lazily evaluatedmessage. If the message does not need lazy evaluation, use thelog(Logger, String)method. This overload constructs expensive messages lazily only when exception is caught. Ifmessagethrows, 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
Errors. IfInterruptedExceptionis caught,Thread.interrupt()is called.- Parameters:
-
logger- where all exceptions are logged -
message- aSupplierthat is lazily evaluated to generate log message - Returns:
- exception handler with custom logger and lazily evaluated message
- Throws:
-
NullPointerException- ifloggerormessageisnull - See Also:
-
log(Logger, String)
-
silence
public static ExceptionHandler silence()
ReturnsExceptionHandlerthat silently ignores all exceptions. This handler is useful when some code is known to produce junk exceptions. Most application code should use logging or counting handler instead.Typical usage:
Exceptions.silence().run(() -> my_throwing_lambda)No exceptions are allowed through, not even
Errors. IfInterruptedExceptionis caught,Thread.interrupt()is called.- Returns:
- exception handler that ignores all exceptions
-
sneak
public static CheckedExceptionHandler sneak()
ReturnsCheckedExceptionHandlerthat 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. Usewrap()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.
- Returns:
- exception handler that lets checked exceptions through
- See Also:
-
wrap()
-
wrap
public static CheckedExceptionHandler wrap()
ReturnsCheckedExceptionHandlerthat wraps all checked exceptions. Unchecked exceptions are passed through unmodified. Checked exceptions are wrapped inUndeclaredThrowableException. Usesneak()to avoid wrapping. Usewrap(Function)to specify a custom wrapper.Typical usage:
Exceptions.wrap().run(() -> my_throwing_lambda)If
InterruptedExceptionis caught,Thread.interrupt()is called.- Returns:
- exception handler that wraps checked exceptions
- See Also:
-
sneak(),wrap(Function)
-
wrap
public static CheckedExceptionHandler wrap(Function<Exception,RuntimeException> wrapper)
CreatesCheckedExceptionHandlerthat applies custom wrapper to checked exceptions. Unchecked exceptions are passed through unmodified. Checked exceptions are passed towrapperand the resulting unchecked exception is thrown. Usesneak()to avoid wrapping andwrap()to use standard wrapper.Typical usage:
Exceptions.wrap(MyWrapperException::new).run(() -> my_throwing_lambda)If
InterruptedExceptionis caught,Thread.interrupt()is called before invoking thewrapper.- Parameters:
-
wrapper- method converting checked exception into an unchecked one, often just exception constructor reference - Returns:
- exception handler with custom exception transform
- Throws:
-
NullPointerException- ifwrapperisnull - See Also:
-
sneak(),wrap()
-
-