Class Exceptions

Object
Static
Exceptions

public final class Exceptions extends Static
Static methods working with Exception instances.
Since:
0.3
  • Method Details

    • getLocalizedMessage

      public static String getLocalizedMessage(Throwable exception, Locale locale)
      Returns the message of the given exception, localized in the given locale if possible. Some exceptions created by SIS can format a message in different locales. This method returns such localized message if possible, or fallback on the standard JDK methods otherwise. More specifically:
      Parameters:
      exception - the exception from which to get the localize message, or null.
      locale - the preferred locale for the message, or null for the JVM default locale. This locale is honored on a best-effort basis only.
      Returns:
      the message in the given locale if possible, or null if the exception argument was null or if the exception does not contain a message.
      See Also:
    • messageEquals

      public static boolean messageEquals(Throwable first, Throwable second)
      Returns true if the given exceptions are of the same class and contains the same message. This method does not compare the stack trace, cause or suppressed exceptions.
      Parameters:
      first - the first exception, or null.
      second - the second exception, or null.
      Returns:
      true if both exceptions are null, or both exceptions are non-null, of the same class and with the same message.
      Since:
      1.0
    • formatChainedMessages

      public static String formatChainedMessages(Locale locale, String header, Throwable cause)
      Returns a string which contains the given message on the first line, followed by the localized message of the given exception on the next line. If the exception has a causes, then the class name and the localized message of the cause are formatted on the next line and the process is repeated for the whole cause chain, omitting duplicated messages.

      SQLException is handled especially in order to process the next exception instead of the cause.

      This method does not format the stack trace.

      Parameters:
      locale - the preferred locale for the exception message, or null.
      header - the message to insert on the first line, or null if none.
      cause - the exception, or null if none.
      Returns:
      the formatted message, or null if both the header was null and no exception provide a message.
    • unwrap

      public static Exception unwrap(Exception exception)
      If the given exception is a wrapper for another exception, returns the unwrapped exception. Otherwise returns the given argument unchanged. An exception is considered a wrapper if:
      Note: Privileged­Action­Exception is also a wrapper exception, but is not included in above list because it is used in very specific contexts. Furthermore, classes related to security manager are deprecated since Java 17.
      This method uses only the exception class as criterion; it does not verify if the exception messages are the same.
      Parameters:
      exception - the exception to unwrap (may be null.
      Returns:
      the unwrapped exception (may be the given argument itself).
      Since:
      0.8