Class UncheckedException
- 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
ConstructorsConstructorDescriptionUncheckedException(String message, Throwable cause) Creates anUncheckedExceptionwith the provided custom message, wrapping the providedThrowable.UncheckedException(Throwable cause) Creates anUncheckedExceptionwrapping the providedThrowable. -
Method Summary
Modifier and TypeMethodDescriptiongetCause()CallsgetCause()on the wrapped exception.Returns anOptionalcontaining the custom message passed in through the two-arg constructor, or an emptyOptionalif the single-arg constructor was used.CallsgetLocalizedMessage()on the wrapped exceptionCallsgetMessage()on the wrapped exceptionCallsgetStackTrace()on the wrapped exceptionThrows anUnsupportedOperationException.voidCallsprintStackTrace()on the wrapped exceptionvoidCallsprintStackTrace()on the wrapped exceptionvoidCallsprintStackTraceon the wrapped exception.voidsetStackTrace(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 anUncheckedExceptionwrapping the providedThrowable.- Parameters:
cause- the exception to wrap and mask
-
UncheckedException
-
-
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
-
getMessage
CallsgetMessage()on the wrapped exception- Overrides:
getMessagein classThrowable- Returns:
- the result of calling
getMessage()on the wrapped exception
-
getLocalizedMessage
CallsgetLocalizedMessage()on the wrapped exception- Overrides:
getLocalizedMessagein classThrowable- Returns:
- the result of calling
getLocalizedMessage()on the wrapped exception
-
getCause
-
printStackTrace
public void printStackTrace()CallsprintStackTrace()on the wrapped exception- Overrides:
printStackTracein classThrowable
-
printStackTrace
CallsprintStackTrace()on the wrapped exception- Overrides:
printStackTracein classThrowable
-
printStackTrace
CallsprintStackTraceon the wrapped exception.- Overrides:
printStackTracein classThrowable
-
getStackTrace
CallsgetStackTrace()on the wrapped exception- Overrides:
getStackTracein classThrowable- Returns:
- the result of calling
getStackTrace()on the wrapped exception
-
setStackTrace
Throws anUnsupportedOperationException.- Overrides:
setStackTracein classThrowable
-
toString
-
initCause
-