Class Exceptions


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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String formatLocalizedMessagesAsString​(java.lang.Throwable throwable, java.util.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.
      static java.lang.String formatStackTraceAsString​(java.lang.Throwable throwable, java.util.Locale locale)
      Returns a String containing the full stack trace of the specified Throwable and all its causes.
      static java.lang.Throwable getCauseByType​(java.lang.Throwable root, java.lang.Class<? extends java.lang.Throwable> causeType)
      Returns the root cause by the given causeType, if existing.
      static java.lang.String getLocalizedMessage​(java.lang.Throwable throwable, java.util.Locale locale)
      Returns the localized message of the specified Throwable.
      static java.lang.String[] getLocalizedMessages​(java.lang.Throwable throwable, java.util.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.
      static boolean isCausedBy​(java.lang.Throwable root, java.lang.Class<? extends java.lang.Throwable> causeType)
      Determines if the given root is caused by the given causeType.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • isCausedBy

        public static boolean isCausedBy​(java.lang.Throwable root,
                                         java.lang.Class<? extends java.lang.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 java.lang.Throwable getCauseByType​(java.lang.Throwable root,
                                                         java.lang.Class<? extends java.lang.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 java.lang.String getLocalizedMessage​(java.lang.Throwable throwable,
                                                           java.util.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 java.lang.String[] getLocalizedMessages​(java.lang.Throwable throwable,
                                                              java.util.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 java.lang.String formatLocalizedMessagesAsString​(java.lang.Throwable throwable,
                                                                       java.util.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 java.lang.String formatStackTraceAsString​(java.lang.Throwable throwable,
                                                                java.util.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