Class ThrowableSuppressor


  • public final class ThrowableSuppressor
    extends java.lang.Object
    Manage multi-exception chains by adding additional throwables as suppressed.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.LinkedList<java.lang.Throwable> throwables  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void attachToThrowable​(java.lang.Throwable throwable)
      Attach all recorded exceptions to another throwable as suppressed exceptions.
      <T> T suppressAppend​(CheckedSupplier<T> supplier, T defaultValue)
      Run a piece of code and record any thrown exception at the end of the exception chain.
      void suppressAppend​(Unchecked.CheckedRunnable runnable)
      Run a piece of code and record any thrown exception at the end of the exception chain.
      <T> T suppressPrepend​(CheckedSupplier<T> supplier, T defaultValue)
      Run a piece of code and record any thrown exception at the beginning of the exception chain.
      void suppressPrepend​(Unchecked.CheckedRunnable runnable)
      Run a piece of code and record any thrown exception at the beginning of the exception chain.
      void throwIfNecessary()
      If any throwables were recorded, use the first as the primary exception and attach all other suppressed exceptions to it.
      <T extends java.lang.Throwable>
      void
      throwIfNecessary​(java.util.function.Function<java.lang.Throwable,​T> function)
      Attach all recorded exceptions to a new throwable and throw it.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • throwables

        private final java.util.LinkedList<java.lang.Throwable> throwables
    • Constructor Detail

      • ThrowableSuppressor

        public ThrowableSuppressor()
    • Method Detail

      • suppressAppend

        public <T> T suppressAppend​(CheckedSupplier<T> supplier,
                                    T defaultValue)
        Run a piece of code and record any thrown exception at the end of the exception chain.
        Type Parameters:
        T - The return value type.
        Parameters:
        supplier - Code to execute.
        defaultValue - Default value if the code throws an exception.
        Returns:
        The output of the executed code or the default value in case of an exception.
      • suppressAppend

        public void suppressAppend​(Unchecked.CheckedRunnable runnable)
        Run a piece of code and record any thrown exception at the end of the exception chain.
        Parameters:
        runnable - Code to execute.
      • suppressPrepend

        public <T> T suppressPrepend​(CheckedSupplier<T> supplier,
                                     T defaultValue)
        Run a piece of code and record any thrown exception at the beginning of the exception chain.
        Type Parameters:
        T - The return value type.
        Parameters:
        supplier - Code to execute.
        defaultValue - Default value if the code throws an exception.
        Returns:
        The output of the executed code or the default value in case of an exception.
      • suppressPrepend

        public void suppressPrepend​(Unchecked.CheckedRunnable runnable)
        Run a piece of code and record any thrown exception at the beginning of the exception chain.
        Parameters:
        runnable - Code to execute.
      • attachToThrowable

        public void attachToThrowable​(java.lang.Throwable throwable)
        Attach all recorded exceptions to another throwable as suppressed exceptions.
        Parameters:
        throwable - The throwable to attach to.
      • throwIfNecessary

        public <T extends java.lang.Throwable> void throwIfNecessary​(java.util.function.Function<java.lang.Throwable,​T> function)
                                                              throws T extends java.lang.Throwable
        Attach all recorded exceptions to a new throwable and throw it.
        Parameters:
        function - Returns the throwable to attach to. Only called if there are any additional throwables present. The first one present is passed into the function and is intended as the cause for the exception chain.
        Throws:
        T extends java.lang.Throwable
      • throwIfNecessary

        public void throwIfNecessary()
        If any throwables were recorded, use the first as the primary exception and attach all other suppressed exceptions to it.