Class RootException

All Implemented Interfaces:
Serializable

public final class RootException extends RuntimeException
A RuntimeException that behaves as though it is the root cause of the provided exception. It wraps the root cause of the provided exception rather than the provided exception itself. It overrides all methods from Exception by delegating to the same method on the root cause. For example, getCause() calls getCause() on the root cause, and thus returns null. This makes the stack trace of a RootException very small and informative, at the cost of not knowing how the original exception (the root cause) bubbled up.
See Also:
  • Constructor Details

    • RootException

      public RootException(Throwable cause)
      Creates a RootException wrapping the root cause of the provided Exception.
      Parameters:
      cause - the exception whose root cause to wrap
    • RootException

      public RootException(String message, Throwable cause)
      Creates a RootException with a custom message.
      Parameters:
      message - a custom message
      cause - the exception to wrap
  • Method Details

    • unwrap

      public <E extends Throwable> E unwrap()
      Returns the exception wrapped by this RootException. Note that getCause() does not return that exception. It returns the cause of the root cause (i.e. null).
      Type Parameters:
      E - The type of the exception
      Returns:
      the exception directly wrapped by this UncheckedException
    • getCustomMessage

      public Optional<String> getCustomMessage()
      Returns an Optional containing the custom message passed in through the two-arg constructor, or an empty Optional if the single-arg constructor was used.
      Returns:
      an Optional containing the custom message passed in through the constructor
    • getMessage

      public String getMessage()
      Calls getMessage() on the root cause.
      Overrides:
      getMessage in class Throwable
      Returns:
      the result of calling getMessage() on the root cause
    • getLocalizedMessage

      public String getLocalizedMessage()
      Calls getLocalizedMessage() on the root cause.
      Overrides:
      getLocalizedMessage in class Throwable
      Returns:
      the result of calling getLocalizedMessage() on the root cause
    • getCause

      public Throwable getCause()
      Calls getCause() on the root exception.
      Overrides:
      getCause in class Throwable
      Returns:
      the result of calling getCause() on the root exception, so null!
    • printStackTrace

      public void printStackTrace()
      Calls printStackTrace() on the root exception.
      Overrides:
      printStackTrace in class Throwable
    • printStackTrace

      public void printStackTrace(PrintStream s)
      Calls printStackTrace() on the root exception.
      Overrides:
      printStackTrace in class Throwable
    • printStackTrace

      public void printStackTrace(PrintWriter s)
      Calls printStackTrace() on the wrapped exception
      Overrides:
      printStackTrace in class Throwable
    • getStackTrace

      public StackTraceElement[] getStackTrace()
      Calls getStackTrace() on the root exception.
      Overrides:
      getStackTrace in class Throwable
      Returns:
      the result of calling getStackTrace() on the root exception
    • toString

      public String toString()
      Calls toString() on the root exception.
      Overrides:
      toString in class Throwable
      Returns:
      the result of calling toString() on the root exception
    • initCause

      public Throwable initCause(Throwable cause)
      Throws an UnsupportedOperationException.
      Overrides:
      initCause in class Throwable
    • setStackTrace

      public void setStackTrace(StackTraceElement[] stackTrace)
      Throws an UnsupportedOperationException.
      Overrides:
      setStackTrace in class Throwable