Class Exceptions
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringformatChainedMessages(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.static StringgetLocalizedMessage(Throwable exception, Locale locale) Returns the message of the given exception, localized in the given locale if possible.static booleanmessageEquals(Throwable first, Throwable second) Returnstrueif the given exceptions are of the same class and contains the same message.static ExceptionIf the given exception is a wrapper for another exception, returns the unwrapped exception.
-
Method Details
-
getLocalizedMessage
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:- If the given
exceptionis null, then this method returnsnull. - Otherwise if the given
localeis null, then this method returnsThrowable.getMessage(). This is consistent with theLocalizedpolicy saying that null locale stands for "unlocalized" message (usually in English) or message in the JVM default locale. - Otherwise if the given
exceptionis an instance ofLocalizedExceptionproviding a non-null international message, then this method returns the result ofInternationalString.toString(Locale). - Otherwise this method returns
Throwable.getLocalizedMessage().
- Parameters:
exception- the exception from which to get the localize message, ornull.locale- the preferred locale for the message, ornullfor the JVM default locale. This locale is honored on a best effort basis only.- Returns:
- the message in the given locale if possible, or
nullif theexceptionargument wasnullor if the exception does not contain a message. - See Also:
- If the given
-
messageEquals
Returnstrueif 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, ornull.second- the second exception, ornull.- Returns:
trueif both exceptions arenull, or both exceptions are non-null, of the same class and with the same message.- Since:
- 1.0
-
formatChainedMessages
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. The process is repeated for the whole cause chain, omitting duplicated messages. This method does not format the stack trace.Special cases
SQLExceptionis handled is a special way by giving precedence toSQLException.getNextException()overThrowable.getCause().When to use
This method should not be used when the given exception will be reported through, for example,Throwable.initCause(Throwable)orLogRecord.setThrown(Throwable), because the redundancy may be confusing. This method is rather for situations where the exception will be discarded or hidden.- Parameters:
locale- the preferred locale for the exception message, ornull.header- the message to insert on the first line, ornullif none.cause- the exception, ornullif none.- Returns:
- the formatted message, or
nullif both the header wasnulland no exception provide a message.
-
unwrap
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:- It is an instance of
InvocationTargetException(could be wrapping anything). - It is an instance of
ExecutionException(could be wrapping anything). - It is an instance of
BackingStoreException(typically wrapping a checked exception). - It is an instance of
UncheckedIOException(wrapping aIOException). - It is an instance of
DirectoryIteratorException(wrapping aIOException). - It is a parent type of its cause. For example, some JDBC drivers wrap
SQLExceptionin anotherSQLExceptionwithout additional information. When the wrapper is a parent class of the cause, details about the reason are less accessible.
- Parameters:
exception- the exception to unwrap (may benull.- Returns:
- the unwrapped exception (may be the given argument itself).
- Since:
- 0.8
- It is an instance of
-