Package org.topbraid.jenax.util
Class ExceptionUtil
- java.lang.Object
-
- org.topbraid.jenax.util.ExceptionUtil
-
public class ExceptionUtil extends Object
A collection of utilities on Exception handling.
-
-
Constructor Summary
Constructors Constructor Description ExceptionUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <EX extends Throwable>
EXgetDeepCause(Throwable t, Class<? extends EX> clazz)static StringgetDeepMessage(Throwable t)Returns the given throwable's message, or the message from its cause if the given throwable has no own message.static StringgetStackTrace(Throwable t)static 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 RuntimeExceptionthrowRootCauseUnchecked(Throwable t)Does not return: throws an unchecked exception, based ont.static RuntimeExceptionthrowUnchecked(Throwable t)Does not return: throws an unchecked exception, based ont.static ThrowablewithServletContainerStackOmitted(Throwable t)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.
-
-
-
Method Detail
-
throwRootCauseUnchecked
public static RuntimeException throwRootCauseUnchecked(Throwable t)
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
public static RuntimeException throwUnchecked(Throwable t)
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 extends Throwable
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).EX extends Throwable
-
getDeepMessage
public static String getDeepMessage(Throwable t)
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:
Throwable.getMessage(),Throwable.getCause()
-
hasDeepCause
public static boolean hasDeepCause(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.
-
getDeepCause
public static <EX extends Throwable> EX getDeepCause(Throwable t, Class<? extends EX> clazz)
-
shortenStackTrace
public static String shortenStackTrace(String stackTrace, String shallowestInterestingMethod)
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
public static String shortenStackTraceForServletCall(String stackTrace)
Returns a shortened form of the given stack trace, omitting lines related to the servlet container before entry into the servlet implementation.- See Also:
shortenStackTrace(String, String)
-
withServletContainerStackOmitted
public static Throwable withServletContainerStackOmitted(Throwable t)
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:
shortenStackTrace(String, String)
-
-