Class ExceptionUtil

java.lang.Object
org.topbraid.jenax.util.ExceptionUtil

public class ExceptionUtil extends Object
A collection of utilities on Exception handling.
  • Constructor Details

    • ExceptionUtil

      public ExceptionUtil()
  • Method Details

    • throwRootCauseUnchecked

      public static RuntimeException throwRootCauseUnchecked(Throwable t)
      Does not return: throws an unchecked exception, based on t. First, the getCause chain of t is 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 Error
      RuntimeException - Otherwise
    • throwUnchecked

      public static RuntimeException throwUnchecked(Throwable t)
      Does not return: throws an unchecked exception, based on t. If t can be thrown directly, it is, otherwise it is wrapped as a RuntimeException.
      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 Error
      RuntimeException - Otherwise
    • throwDeepCauseChecked

      public static <EX extends Throwable> EX throwDeepCauseChecked(Throwable t, Class<? extends EX> clazz) throws EX
      Always throw an exception; based on t. The getCause chain of t is analyzed, and then, an appropriate Throwable is thrown, with preference as follows:
      1. An Error.
      2. An exception that is of type clazz (or a subtype).
      3. A runtime exception.
      4. A new Exception of type clazz that has cause t
      If one of the first three is thrown, then it is the first one of that type in the causal chain.
      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 Error
      RuntimeException - If there is an underlying RuntimeException and no exception of type EX
      EX - If there is an appropriate exception or otherwise
      IllegalArgumentException - If clazz is inappropriate (not always checked).
    • 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:
    • getStackTrace

      public static String getStackTrace(Throwable t)
    • hasDeepCause

      public static boolean hasDeepCause(Throwable t, Class<? extends Throwable> throwableClass)
      True if the given throwable or one of its causes (via Throwable.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 by getStackTrace(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:
    • 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: