- All Implemented Interfaces:
Serializable
RuntimeException
that behaves just like Exception
it wraps. it
overrides all methods from Exception
by delegating to the same
method on the wrapped exception. For example, getCause()
does not return
the wrapped exception but the cause of the wrapped exception. You can
provide a custom message that pertains to UncheckedException
itself, but
it must be retrieved via getCustomMessage()
as getMessage()
would return the message of the wrapped exception.
This behaviour can be useful when wrapping checked exceptions that in
practice cannot sensibly be dealt with. This is often the case with, for example,
IOException
, SQLException
and other exceptions where the Javadocs
state that they are thrown "when something goes wrong". These exceptions are
runtime exceptions for all practical purposes.
By hiding completely behind the wrapped exception, an
UncheckedException
has a less cumbersome stack trace than a straight
RuntimeException
. Beware of surprises though, as the only way of knowing
you are dealing with an UncheckedException
is by calling
getClass()
on it.
An UncheckedException
can be safely wrapped into an
UncheckedException
. The constructors "bore through" the causes until they
find something that is not an UncheckedException
and not a
RootException
.
- See Also:
-
Constructor Summary
ConstructorDescriptionUncheckedException
(String message, Throwable cause) Creates anUncheckedException
with the provided custom message, wrapping the providedThrowable
.UncheckedException
(Throwable cause) Creates anUncheckedException
wrapping the providedThrowable
. -
Method Summary
Modifier and TypeMethodDescriptiongetCause()
CallsgetCause()
on the wrapped 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 wrapped exceptionCallsgetMessage()
on the wrapped exceptionCallsgetStackTrace()
on the wrapped exceptionThrows anUnsupportedOperationException
.void
CallsprintStackTrace()
on the wrapped exceptionvoid
CallsprintStackTrace()
on the wrapped exceptionvoid
CallsprintStackTrace
on the wrapped exception.void
setStackTrace
(StackTraceElement[] stackTrace) Throws anUnsupportedOperationException
.toString()
CallstoString()
on the wrapped exception.<E extends Throwable>
Eunwrap()
Returns the exception wrapped by thisUncheckedException
.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getSuppressed
-
Constructor Details
-
UncheckedException
Creates anUncheckedException
wrapping the providedThrowable
.- Parameters:
cause
- the exception to wrap and mask
-
UncheckedException
Creates anUncheckedException
with the provided custom message, wrapping the providedThrowable
.- Parameters:
message
- a custom messagecause
- the exception to wrap and mask
-
-
Method Details
-
unwrap
Returns the exception wrapped by thisUncheckedException
. Note thatgetCause()
does not return that exception. It returns the cause of the cause.- 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 wrapped exception- Overrides:
getMessage
in classThrowable
- Returns:
- the result of calling
getMessage()
on the wrapped exception
-
getLocalizedMessage
CallsgetLocalizedMessage()
on the wrapped exception- Overrides:
getLocalizedMessage
in classThrowable
- Returns:
- the result of calling
getLocalizedMessage()
on the wrapped exception
-
getCause
CallsgetCause()
on the wrapped exception. -
printStackTrace
public void printStackTrace()CallsprintStackTrace()
on the wrapped exception- Overrides:
printStackTrace
in classThrowable
-
printStackTrace
CallsprintStackTrace()
on the wrapped exception- Overrides:
printStackTrace
in classThrowable
-
printStackTrace
CallsprintStackTrace
on the wrapped exception.- Overrides:
printStackTrace
in classThrowable
-
getStackTrace
CallsgetStackTrace()
on the wrapped exception- Overrides:
getStackTrace
in classThrowable
- Returns:
- the result of calling
getStackTrace()
on the wrapped exception
-
setStackTrace
Throws anUnsupportedOperationException
.- Overrides:
setStackTrace
in classThrowable
-
toString
CallstoString()
on the wrapped exception. -
initCause
Throws anUnsupportedOperationException
.
-