Class BaseException
- All Implemented Interfaces:
Serializable
Standard base class of all checked Exceptions.
A BaseException
always has an ErrorCode
. The format string of the exception will be loaded from a
corresponding ResourceBundle
and formatted with MessageFormat.format(String, Object...)
using the
specified parameters.
There are several methods with localization support that accept a Locale
. All other methods assume
Locale.ENGLISH
as the default or use the saved message that was created with Locale.ENGLISH
by the
constructor.
The ResourceBundle
s are always read from property-files. Their base name is the product name of the
ErrorCode
(in lower case) concatenated with the string "errorcodes". The resource key is the written form of
the error code as returned by ErrorCode.getString()
.
It is recommended for all products to extend BaseException
with their own product-specific base exception and
to extend it further with specialized exceptions. It is also recommended for all products to create their own
product-specific extension of ErrorCode
encapsulating the product-name-part of the error code.
Usage examples:
1) general product-specific exception or a more specific but still generic exception, that can have different error
codes and parameters:
public class MyProductException extends BaseException { public MyProductException(MyProductErrorCode errorCode, String[] arParameters, Throwable cause) { super(errorCode, arParameters, cause); } public MyProductException(MyProductErrorCode errorCode, String[] arParameters) { super(errorCode, arParameters); } }2) A special exception, that always has the same error code and a message and has parameters:
public class MySpecialException extends BaseException { private static final MyProductErrorCode ERROR_CODE = new MyProductErrorCode(4711); public MySpecialException(MyObjectClass1 myObject1, MyObjectClass2 myObject2, Throwable cause) { super(ERROR_CODE, convert(myObject1, myObject2), cause); } public MySpecialException(MyObjectClass1 myObject1, MyObjectClass2 myObject2) { super(ERROR_CODE, convert(myObject1, myObject2)); } private static String[] convert(MyObjectClass1 myObject1, MyObjectClass2 myObject2) { String[] arStrings = new String[2]; if (myObject1 == null) { arStrings[0] = null; } else { arStrings[0] = myObject1.toString(); } if (myObject2 == null) { arStrings[1] = null; } else { arStrings[1] = myObject2.toString(); } return arStrings; } }3) A very special exception, that always has the same error code and a message without parameters and thus is easy to use:
public class MyVerySpecialException extends BaseException { private static final MyProductErrorCode ERROR_CODE = new MyProductErrorCode(4711); public MyVerySpecialException(Throwable cause) { super(ERROR_CODE, null, cause); } public MyVerySpecialException() { super(ERROR_CODE, null); } }
- Author:
- agz
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionBaseException
(ErrorCode errorCode, String[] arParameters) Creates a newBaseException
with the specifiedErrorCode
and parameters and no cause.BaseException
(ErrorCode errorCode, String[] arParameters, Throwable cause) Creates a newBaseException
with the specifiedErrorCode
, parameters and cause. -
Method Summary
Modifier and TypeMethodDescriptionfinal Throwable
getCause()
final String
final String
getLocalizedMessage
(Locale locale) Tries to read a localized format string from aResourceBundle
for the specifiedLocale
and uses the parameters specified in the constructor to create a resulting localized message and returns this message.final String[]
getLocalizedMessages
(Locale locale) Returns an array of the messages of this exception and all its causes, and tries to get localized messages the same way asgetLocalizedMessage(Locale)
would do.Method is not supported and throws anUnsupportedOperationException
always.final void
printStackTrace
(PrintStream printStream, Locale locale) Prints the stack trace to the specified print stream, but tries to get a localized message for this exception and all cause exceptions the same way asgetLocalizedMessage(Locale)
would do.final void
printStackTrace
(PrintWriter printWriter, Locale locale) Prints the stack trace to the specified print writer, but tries to get a localized message for this exception and all cause exceptions the same way asgetLocalizedMessage(Locale)
would do.final void
printStackTrace
(Locale locale) Prints the stack trace to the standard error stream, but tries to get a localized message for this exception and all cause exceptions the same way asgetLocalizedMessage(Locale)
would do.protected final void
Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getMessage, getStackTrace, getSuppressed, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Constructor Details
-
BaseException
Creates a newBaseException
with the specifiedErrorCode
, parameters and cause. TheErrorCode
must not benull
.
Since throwing an exception while creating an exception would destroy all information about the indeed error, it will work even ifnull
is specified, but has no meaningful error number and message. A message indicating that the error code was not specified will be created instead. The parameter array is used directly (not cloned), so it should not be modified/used after assignment to this exception. The parameters are optional (may benull
). The cause is optional (may benull
). -
BaseException
Creates a newBaseException
with the specifiedErrorCode
and parameters and no cause. TheErrorCode
must not benull
.
Since throwing an exception while creating an exception would destroy all information about the indeed error, it will work even ifnull
is specified, but has no error number and message. A message indicating that the error code was not specified will be created instead. The parameter array is used directly (not cloned), so it should not be modified/used after assignment to this exception. The parameters are optional (may benull
).- Parameters:
errorCode
-ErrorCode
of the new exceptionarParameters
- message parameters of the new exception
-
-
Method Details
-
initCause
Method is not supported and throws anUnsupportedOperationException
always. -
setCause
-
getCause
-
getLocalizedMessage
Since this method has noLocale
parameter and the systems default locale makes no sense in most situations, the behavior is unchanged for external calls, i.e. unlocalized. UsegetLocalizedMessage(Locale)
to get a localized message instead!- Overrides:
getLocalizedMessage
in classThrowable
- See Also:
-
getLocalizedMessage
Tries to read a localized format string from aResourceBundle
for the specifiedLocale
and uses the parameters specified in the constructor to create a resulting localized message and returns this message. If noResourceBundle
for the specifiedLocale
exists it falls back to the defaultResourceBundle
.- Parameters:
locale
-Locale
for which to create a localized message- Returns:
- localized message or default message, if localization is not possible
-
printStackTrace
Prints the stack trace to the standard error stream, but tries to get a localized message for this exception and all cause exceptions the same way asgetLocalizedMessage(Locale)
would do.- Parameters:
locale
-Locale
for which to create a localized stack trace- See Also:
-
printStackTrace
Prints the stack trace to the specified print stream, but tries to get a localized message for this exception and all cause exceptions the same way asgetLocalizedMessage(Locale)
would do.- Parameters:
printStream
-PrintStream
to use for outputlocale
-Locale
for which to create a localized stack trace- See Also:
-
printStackTrace
Prints the stack trace to the specified print writer, but tries to get a localized message for this exception and all cause exceptions the same way asgetLocalizedMessage(Locale)
would do.- Parameters:
printWriter
-PrintWriter
to use for outputlocale
-Locale
for which to create a localized stack trace- See Also:
-
getLocalizedMessages
Returns an array of the messages of this exception and all its causes, and tries to get localized messages the same way asgetLocalizedMessage(Locale)
would do. The "topmost" message (at index == 0) is the localized message of this exception. Higher indexes are deeper causes.- Parameters:
locale
-Locale
for which to create an array of localized messages- Returns:
- array of localized messages
-