Class BaseException

java.lang.Object
java.lang.Throwable
java.lang.Exception
com.saperion.common.lang.exception.BaseException
All Implemented Interfaces:
Serializable

public abstract class BaseException extends Exception

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 ResourceBundles 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 Details

    • BaseException

      public BaseException(ErrorCode errorCode, String[] arParameters, Throwable cause)
      Creates a new BaseException with the specified ErrorCode, parameters and cause.

      The ErrorCode must not be null.
      Since throwing an exception while creating an exception would destroy all information about the indeed error, it will work even if null 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 be null).

      The cause is optional (may be null).

      Parameters:
      errorCode - ErrorCode of the new exception
      arParameters - message parameters of the new exception
      cause - cause-Throwable of the new exception
    • BaseException

      public BaseException(ErrorCode errorCode, String[] arParameters)
      Creates a new BaseException with the specified ErrorCode and parameters and no cause.

      The ErrorCode must not be null.
      Since throwing an exception while creating an exception would destroy all information about the indeed error, it will work even if null 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 be null).

      Parameters:
      errorCode - ErrorCode of the new exception
      arParameters - message parameters of the new exception
  • Method Details