Class BaseException
- java.lang.Object
-
- java.lang.Throwable
-
- java.lang.Exception
-
- com.saperion.common.lang.exception.BaseException
-
- All Implemented Interfaces:
java.io.Serializable
public abstract class BaseException extends java.lang.Exception
Standard base class of all checked Exceptions.
A
BaseException
always has anErrorCode
. The format string of the exception will be loaded from a correspondingResourceBundle
and formatted withMessageFormat.format(String, Object...)
using the specified parameters.There are several methods with localization support that accept a
Locale
. All other methods assumeLocale.ENGLISH
as the default or use the saved message that was created withLocale.ENGLISH
by the constructor.The
ResourceBundle
s are always read from property-files. Their base name is the product name of theErrorCode
(in lower case) concatenated with the string "errorcodes". The resource key is the written form of the error code as returned byErrorCode.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 ofErrorCode
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:
ErrorCode
,PortableException
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description BaseException(ErrorCode errorCode, java.lang.String[] arParameters)
Creates a newBaseException
with the specifiedErrorCode
and parameters and no cause.BaseException(ErrorCode errorCode, java.lang.String[] arParameters, java.lang.Throwable cause)
Creates a newBaseException
with the specifiedErrorCode
, parameters and cause.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Throwable
getCause()
java.lang.String
getLocalizedMessage()
java.lang.String
getLocalizedMessage(java.util.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.java.lang.String[]
getLocalizedMessages(java.util.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.java.lang.Throwable
initCause(java.lang.Throwable cause)
Method is not supported and throws anUnsupportedOperationException
always.void
printStackTrace(java.io.PrintStream printStream, java.util.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.void
printStackTrace(java.io.PrintWriter printWriter, java.util.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.void
printStackTrace(java.util.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 void
setCause(java.lang.Throwable cause)
-
-
-
Constructor Detail
-
BaseException
public BaseException(ErrorCode errorCode, java.lang.String[] arParameters, java.lang.Throwable cause)
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
).- Parameters:
errorCode
-ErrorCode
of the new exceptionarParameters
- message parameters of the new exceptioncause
- cause-Throwable
of the new exception
-
BaseException
public BaseException(ErrorCode errorCode, java.lang.String[] arParameters)
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 Detail
-
initCause
public java.lang.Throwable initCause(java.lang.Throwable cause)
Method is not supported and throws anUnsupportedOperationException
always.- Overrides:
initCause
in classjava.lang.Throwable
-
setCause
protected final void setCause(java.lang.Throwable cause)
-
getCause
public final java.lang.Throwable getCause()
- Overrides:
getCause
in classjava.lang.Throwable
-
getLocalizedMessage
public final java.lang.String 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 classjava.lang.Throwable
- See Also:
getLocalizedMessage(Locale)
-
getLocalizedMessage
public final java.lang.String getLocalizedMessage(java.util.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. 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
public final void printStackTrace(java.util.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.- Parameters:
locale
-Locale
for which to create a localized stack trace- See Also:
Throwable.printStackTrace()
,getLocalizedMessage(Locale)
-
printStackTrace
public final void printStackTrace(java.io.PrintStream printStream, java.util.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.- Parameters:
printStream
-PrintStream
to use for outputlocale
-Locale
for which to create a localized stack trace- See Also:
Throwable.printStackTrace(PrintStream)
,getLocalizedMessage(Locale)
-
printStackTrace
public final void printStackTrace(java.io.PrintWriter printWriter, java.util.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.- Parameters:
printWriter
-PrintWriter
to use for outputlocale
-Locale
for which to create a localized stack trace- See Also:
Throwable.printStackTrace(PrintWriter)
,getLocalizedMessage(Locale)
-
getLocalizedMessages
public final java.lang.String[] getLocalizedMessages(java.util.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. 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
-
-