Class 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 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:
    ErrorCode, PortableException, Serialized Form
    • 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 a ResourceBundle for the specified Locale 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 as getLocalizedMessage(Locale) would do.
      java.lang.Throwable initCause​(java.lang.Throwable cause)
      Method is not supported and throws an UnsupportedOperationException 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 as getLocalizedMessage(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 as getLocalizedMessage(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 as getLocalizedMessage(Locale) would do.
      protected void setCause​(java.lang.Throwable cause)  
      • Methods inherited from class java.lang.Throwable

        addSuppressed, fillInStackTrace, getMessage, getStackTrace, getSuppressed, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • BaseException

        public BaseException​(ErrorCode errorCode,
                             java.lang.String[] arParameters,
                             java.lang.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,
                             java.lang.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 Detail

      • initCause

        public java.lang.Throwable initCause​(java.lang.Throwable cause)
        Method is not supported and throws an UnsupportedOperationException always.
        Overrides:
        initCause in class java.lang.Throwable
      • setCause

        protected final void setCause​(java.lang.Throwable cause)
      • getCause

        public final java.lang.Throwable getCause()
        Overrides:
        getCause in class java.lang.Throwable
      • getLocalizedMessage

        public final java.lang.String getLocalizedMessage()

        Since this method has no Locale parameter and the systems default locale makes no sense in most situations, the behavior is unchanged for external calls, i.e. unlocalized.

        Use getLocalizedMessage(Locale) to get a localized message instead!

        Overrides:
        getLocalizedMessage in class java.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 a ResourceBundle for the specified Locale and uses the parameters specified in the constructor to create a resulting localized message and returns this message.

        If no ResourceBundle for the specified Locale exists it falls back to the default ResourceBundle.

        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 as getLocalizedMessage(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 as getLocalizedMessage(Locale) would do.
        Parameters:
        printStream - PrintStream to use for output
        locale - 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 as getLocalizedMessage(Locale) would do.
        Parameters:
        printWriter - PrintWriter to use for output
        locale - 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 as getLocalizedMessage(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