com.machinezoo.noexception

Class Exceptions

NoException » API Reference » com.machinezoo.noexception



  • public final class Exceptions
    extends Object
    Static methods for creating predefined exception handlers. Custom exception handlers can be created by inheriting from 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))
    See Also:
    NoException tutorial
    • Method Detail

      • pass

        public static ExceptionHandler pass()
        Get 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.
        Returns:
        pass-through exception handler
      • log

        public static ExceptionHandler log()
        Get 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 Errors. If InterruptedException is caught, Thread.interrupt() is called.
        Returns:
        logging exception handler
        See Also:
        log(Logger)
      • log

        public static ExceptionHandler log(Logger logger)
        Create 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 Errors. If InterruptedException is caught, Thread.interrupt() is called.
        Parameters:
        logger - where all exceptions are logged
        Returns:
        exception handler with custom logger
        Throws:
        NullPointerException - if logger is null
        See Also:
        log(), log(Logger, String)
      • log

        public static ExceptionHandler log(Logger logger,
                                           String message)
        Create ExceptionHandler that writes all exceptions to the specified logger with the specified message. Most application code can use the more convenient log() method. This overload allows for differentiating or explanatory message. If you just need to specify custom logger, use log(Logger).
        Typical usage: Exceptions.log(logger, "Caught exception").run(() -> my_throwing_lambda)
        No exceptions are allowed through, not even Errors. If InterruptedException is 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 - if logger or message is null
        See Also:
        log(), log(Logger)
      • silence

        public static ExceptionHandler silence()
        Get 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 Errors. If InterruptedException is caught, Thread.interrupt() is called.
        Returns:
        exception handler that ignores all exceptions
        See Also:
        log()
      • sneak

        public static CheckedExceptionHandler sneak()
        Get 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.
        Returns:
        exception handler that lets checked exceptions through
        See Also:
        wrap()
      • wrap

        public static CheckedExceptionHandler wrap(Function<Exception,RuntimeException> wrapper)
        Create 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.
        Parameters:
        wrapper - method converting checked exception into an unchecked one, often just exception constructor reference
        Returns:
        exception handler with custom exception transform
        Throws:
        NullPointerException - if wrapper is null
        See Also:
        sneak(), wrap()