Class Exceptions

java.lang.Object
com.saperion.common.lang.exception.Exceptions

public final class Exceptions extends Object
The class Exceptions provides exception utility methods.
Author:
sts
  • Method Details

    • isCausedBy

      public static boolean isCausedBy(Throwable root, Class<? extends Throwable> causeType)
      Determines if the given root is caused by the given causeType. The whole cause-hierarchy of root is regarded recursively.

      The specified root Throwable must not be null.

      The specified causeType must not be null.

      Parameters:
      root - the root Throwable to inspect.
      causeType - the type of the Throwable cause in question.
      Returns:
      true, if root is caused by the causeType, false otherwise.
    • getCauseByType

      public static Throwable getCauseByType(Throwable root, Class<? extends Throwable> causeType)
      Returns the root cause by the given causeType, if existing. If a cause matches the given causeType is determined by the algorithm: causeType.isInstance(root). The whole cause-hierarchy of root is regarded recursively. The first found cause by the given causeType is returned.

      Returns null if no such cause is found.

      The specified root Throwable must not be null.

      The specified causeType must not be null.

      Parameters:
      root - the root Throwable to inspect.
      causeType - the type of the Throwable cause in question.
      Returns:
      the cause by type if existing or null if not existing.
    • getLocalizedMessage

      public static String getLocalizedMessage(Throwable throwable, Locale locale)
      Returns the localized message of the specified Throwable.

      For BaseExceptions it returns the same as BaseException.getLocalizedMessage(Locale), for all other Throwables the same as Throwable.getLocalizedMessage().

      The specified Throwable must not be null.

      The specified Locale may be null.

      Parameters:
      throwable - Throwable to get an array of localized messages for
      locale - Locale to get the localized messages of BaseExceptions for
      Returns:
      the localized message
    • getLocalizedMessages

      public static String[] getLocalizedMessages(Throwable throwable, Locale locale)
      Returns an array of the messages of the specified Throwable and all its causes, and tries to get localized messages the same way as getLocalizedMessage(Throwable, Locale) would do.

      The "topmost" message (at index == 0) is the localized message of the specified Throwable. Higher indexes are deeper causes.

      The specified Throwable must not be null.

      The specified Locale may be null.

      Parameters:
      throwable - Throwable to get the localized message for
      locale - Locale to get the localized message of BaseExceptions for
      Returns:
      array of localized messages
    • formatLocalizedMessagesAsString

      public static String formatLocalizedMessagesAsString(Throwable throwable, Locale locale)
      Returns a String containing the localized messages of the specified Throwable and all its causes as returned by getLocalizedMessages(Throwable, Locale) separated by a line separator.

      The specified Throwable must not be null.

      The specified Locale may be null.

      Parameters:
      throwable - Throwable to get the localized message for
      locale - Locale to get the localized message of BaseExceptions for
      Returns:
      array of localized messages
    • formatStackTraceAsString

      public static String formatStackTraceAsString(Throwable throwable, Locale locale)
      Returns a String containing the full stack trace of the specified Throwable and all its causes.

      For BaseExceptions the content of the returned String is the same as Throwable.printStackTrace() would produce and for all other Throwables it is analogous to Throwable.printStackTrace().

      The specified Throwable must not be null.

      The specified Locale may be null.

      Parameters:
      throwable - Throwable to get the stack trace for
      locale - Locale to get the localized message of BaseExceptions for
      Returns:
      String containing the full stack trace of the specified Throwable and all its causes