java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
org.klojang.util.exception.RootException
- All Implemented Interfaces:
Serializable
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 Summary
ConstructorDescriptionRootException
(String message, Throwable cause) Creates aRootException
with a custom message.RootException
(Throwable cause) Creates aRootException
wrapping the root cause of the providedException
. -
Method Summary
Modifier and TypeMethodDescriptiongetCause()
CallsgetCause()
on the root exception.Returns anOptional
containing the custom message passed in through the two-arg constructor, or an emptyOptional
if the single-arg constructor was used.CallsgetLocalizedMessage()
on the root cause.CallsgetMessage()
on the root cause.CallsgetStackTrace()
on the root exception.Throws anUnsupportedOperationException
.void
CallsprintStackTrace()
on the root exception.void
CallsprintStackTrace()
on the root exception.void
CallsprintStackTrace()
on the wrapped exceptionvoid
setStackTrace
(StackTraceElement[] stackTrace) Throws anUnsupportedOperationException
.toString()
CallstoString()
on the root exception.<E extends Throwable>
Eunwrap()
Returns the exception wrapped by thisRootException
.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getSuppressed
-
Constructor Details
-
RootException
Creates aRootException
wrapping the root cause of the providedException
.- Parameters:
cause
- the exception whose root cause to wrap
-
RootException
Creates aRootException
with a custom message.- Parameters:
message
- a custom messagecause
- the exception to wrap
-
-
Method Details
-
unwrap
Returns the exception wrapped by thisRootException
. Note thatgetCause()
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
Returns anOptional
containing the custom message passed in through the two-arg constructor, or an emptyOptional
if the single-arg constructor was used.- Returns:
- an
Optional
containing the custom message passed in through the constructor
-
getMessage
CallsgetMessage()
on the root cause.- Overrides:
getMessage
in classThrowable
- Returns:
- the result of calling
getMessage()
on the root cause
-
getLocalizedMessage
CallsgetLocalizedMessage()
on the root cause.- Overrides:
getLocalizedMessage
in classThrowable
- Returns:
- the result of calling
getLocalizedMessage()
on the root cause
-
getCause
CallsgetCause()
on the root exception. -
printStackTrace
public void printStackTrace()CallsprintStackTrace()
on the root exception.- Overrides:
printStackTrace
in classThrowable
-
printStackTrace
CallsprintStackTrace()
on the root exception.- Overrides:
printStackTrace
in classThrowable
-
printStackTrace
CallsprintStackTrace()
on the wrapped exception- Overrides:
printStackTrace
in classThrowable
-
getStackTrace
CallsgetStackTrace()
on the root exception.- Overrides:
getStackTrace
in classThrowable
- Returns:
- the result of calling
getStackTrace()
on the root exception
-
toString
CallstoString()
on the root exception. -
initCause
Throws anUnsupportedOperationException
. -
setStackTrace
Throws anUnsupportedOperationException
.- Overrides:
setStackTrace
in classThrowable
-