Package org.topbraid.jenax.util
Class ExceptionUtil
java.lang.Object
org.topbraid.jenax.util.ExceptionUtil
A collection of utilities on Exception handling.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <EX extends Throwable>
EXgetDeepCause(Throwable t, Class<? extends EX> clazz) static StringReturns the given throwable's message, or the message from its cause if the given throwable has no own message.static Stringstatic booleanhasDeepCause(Throwable t, Class<? extends Throwable> throwableClass) True if the given throwable or one of its causes (viaThrowable.getCause()) is an instance of the given class.static StringshortenStackTrace(String stackTrace, String shallowestInterestingMethod) Returns a shortened form of the given stack trace by removing "boring" lines.static StringshortenStackTraceForServletCall(String stackTrace) Returns a shortened form of the given stack trace, omitting lines related to the servlet container before entry into the servlet implementation.static <EX extends Throwable>
EXthrowDeepCauseChecked(Throwable t, Class<? extends EX> clazz) Always throw an exception; based ont.static RuntimeExceptionDoes not return: throws an unchecked exception, based ont.static RuntimeExceptionDoes not return: throws an unchecked exception, based ont.static ThrowableReturns a version of the given throwable that prints with a shortened stack trace, omitting lines related to the servlet container before entry into the servlet implementation.
-
Constructor Details
-
ExceptionUtil
public ExceptionUtil()
-
-
Method Details
-
throwRootCauseUnchecked
Does not return: throws an unchecked exception, based ont. First, thegetCausechain oftis followed to its base, and then, an appropriate throwable exception is thrown, with preference to throwing an Error.- Parameters:
t- The underlying problem.- Returns:
- Never returns, return type is for idiom "throw throwUnchecked();" to clarify that next line is not reached.
- Throws:
Error- If there is an underlying ErrorRuntimeException- Otherwise
-
throwUnchecked
Does not return: throws an unchecked exception, based ont. Iftcan be thrown directly, it is, otherwise it is wrapped as aRuntimeException.- Parameters:
t- The underlying problem.- Returns:
- Never returns, return type is for idiom "throw throwUnchecked();" to clarify that next line is not reached.
- Throws:
Error- If t is an ErrorRuntimeException- Otherwise
-
throwDeepCauseChecked
public static <EX extends Throwable> EX throwDeepCauseChecked(Throwable t, Class<? extends EX> clazz) throws EX Always throw an exception; based ont. ThegetCausechain oftis analyzed, and then, an appropriate Throwable is thrown, with preference as follows:- An Error.
- An exception that is of type
clazz(or a subtype). - A runtime exception.
- A new Exception of type clazz that has cause
t
- Type Parameters:
EX- The type of exception thrown by the constructor of V- Parameters:
t- The underlying problem.clazz- The class of exception to look for. This clazz must not be abstract and must have either a no element constructor or a constructor with one argument being a throwable.- Returns:
- Never returns, return type is for idiom "throw throwUnchecked();" to clarify that next line is not reached.
- Throws:
Error- If there is an underlying ErrorRuntimeException- If there is an underlying RuntimeException and no exception of type EXEX- If there is an appropriate exception or otherwiseIllegalArgumentException- If clazz is inappropriate (not always checked).
-
getDeepMessage
Returns the given throwable's message, or the message from its cause if the given throwable has no own message. Will return null if no explicit message is provided in the throwable or any of its causes. The intent is to get "The real message" instead of "x.y.SomeException: The real message" when the throwable bearing the real message is wrapped without a new message.- See Also:
-
getStackTrace
-
hasDeepCause
True if the given throwable or one of its causes (viaThrowable.getCause()) is an instance of the given class. -
getDeepCause
-
shortenStackTrace
Returns a shortened form of the given stack trace by removing "boring" lines. The first "interesting" method is given as an argument. All lines below in the stack trace, that is, the caller of the "interesting" method, and its caller, and so on, up to the origination point of the thread, are omitted.- Parameters:
stackTrace- The input stack trace, as produced bygetStackTrace(Throwable)shallowestInterestingMethod- A fully qualified method name, as it appears in a stack trace: org.example.MyClass.myMethod- Returns:
- A shortened stack trace that omits the callers of the class
-
shortenStackTraceForServletCall
Returns a shortened form of the given stack trace, omitting lines related to the servlet container before entry into the servlet implementation.- See Also:
-
withServletContainerStackOmitted
Returns a version of the given throwable that prints with a shortened stack trace, omitting lines related to the servlet container before entry into the servlet implementation. The resulting exception is only intended for printing/logging its stack trace. It is not intended to be thrown or wrapped into other exceptions.- See Also:
-