Class ApplicationException

java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
io.github.mmm.base.exception.ApplicationException
All Implemented Interfaces:
Localizable, LocalizableObject, Serializable
Direct Known Subclasses:
DuplicateObjectException, ObjectMismatchException, ObjectNotFoundException, ReadOnlyException, RuntimeIoException

public abstract class ApplicationException extends RuntimeException implements Localizable
Extends RuntimeException with the following features: NOTE:
Exceptions should only occur in unexpected or undesired situations. Never use exceptions for control flows.
See Also:
  • Constructor Details

    • ApplicationException

      public ApplicationException(String message)
      The constructor.
      Parameters:
      message - the message describing the problem briefly.
    • ApplicationException

      public ApplicationException(Localizable message)
      The constructor.
      Parameters:
      message - the message describing the problem briefly.
    • ApplicationException

      public ApplicationException(String message, Throwable cause)
      The constructor.
      Parameters:
      message - the message describing the problem briefly.
      cause - the cause of this exception.
    • ApplicationException

      public ApplicationException(Localizable message, Throwable cause)
      The constructor.
      Parameters:
      message - the NLS message describing the problem briefly.
      cause - the cause of this exception.
    • ApplicationException

      protected ApplicationException(String message, Throwable cause, UUID uuid)
      The constructor.
      Parameters:
      message - the message describing the problem briefly.
      cause - the cause of this exception. May be null.
      uuid - the explicit UUID or null to initialize by default (from given Throwable or as new UUID).
    • ApplicationException

      protected ApplicationException(Localizable message, Throwable cause, UUID uuid)
      The constructor.
      Parameters:
      message - the NLS message describing the problem briefly.
      cause - the cause of this exception. May be null.
      uuid - the explicit UUID or null to initialize by default (from given Throwable or as new UUID).
  • Method Details

    • createUuid

      protected UUID createUuid()
      Returns:
      a new UUID for getUuid().
    • getUuid

      public final UUID getUuid()
      This method gets the UUID of this ApplicationException. When a new ApplicationException is created, a UUID is assigned. In case the ApplicationException is created from another ApplicationException as cause, the existing UUID will be used from that cause. Otherwise a new UUID is generated.
      The UUID will appear in the stacktrace but NOT in the message. It will therefore be written to log-files if this exception is logged. If you supply the UUID to the end-user, he can provide it with the problem report so an administrator or software developer can easily find the stacktrace in the log-files.
      Returns:
      the UUID of this object.
    • getMessage

      public String getMessage()
      ATTENTION: Logging frameworks like logback do not follow Java exception standards and write toString() but only getMessage() of the Exception together with the stacktrace into the log. Since an ApplicationException contains additional information like UUID and code that needs to be logged we are forced to return the message from this method in the following form:
      [«code»: ]«message»
       «uuid»
       
      Please note that the code is omitted if the getCode() method is not overridden and returning a custom code.
      In case you want to get the plain exception message, you therefore need to either call getLocalizedMessage() or use getNlsMessage().
      Specified by:
      getMessage in interface Localizable
      Overrides:
      getMessage in class Throwable
      Returns:
      the untranslated message in the form specified above.
      See Also:
    • getNlsMessage

      public Localizable getNlsMessage()
      Returns:
      the Localizable message describing the problem.
      See Also:
    • getLocalizedMessage

      public String getLocalizedMessage()
      Description copied from interface: Localizable
      This method tries to get the localized message as String using the default locale. ATTENTION:
      If possible try to avoid using this method and use Localizable.getLocalizedMessage(Locale) instead (e.g. using spring LocaleContextHolder to get the users locale).
      Specified by:
      getLocalizedMessage in interface Localizable
      Overrides:
      getLocalizedMessage in class Throwable
      Returns:
      the localized message.
    • getLocalizedMessage

      public String getLocalizedMessage(Locale locale)
      Description copied from interface: Localizable
      This method gets the resolved and localized message.
      Specified by:
      getLocalizedMessage in interface Localizable
      Parameters:
      locale - is the locale to translate to.
      Returns:
      the localized message.
    • getLocalizedMessage

      public void getLocalizedMessage(Locale locale, Appendable appendable)
      Description copied from interface: Localizable
      This method writes the localized message to the given Appendable.
      Specified by:
      getLocalizedMessage in interface Localizable
      Parameters:
      locale - the Locale to translate to.
      appendable - the Appendable where to write the message to.
      See Also:
    • printStackTrace

      public void printStackTrace(Locale locale, Appendable buffer)
      This method prints the stack trace with localized exception message(s).
      Parameters:
      locale - is the locale to translate to.
      buffer - is where to write the stack trace to.
      Throws:
      IllegalStateException - if the given buffer produced an IOException.
    • isTechnical

      public boolean isTechnical()
      Determines if this is a technical exception.
      • A technical exception is an unexpected situation that is to be logged on error level and should be analyzed by the operators or software developers of the system. Further in such case the end-user can typically do nothing about the problem (except to retry his operation) and will typically not understand the problem. Therefore a generic message should be displayed to the end-user in such case.
      • A non technical exception is called user failure. It is an undesired but NOT abnormal situation (e.g. a mandatory field was not filled). It should be logged on a level less than error (typically info). The message is typically intended for to end-users and has to be easy to understand.
      This separation is intentionally not done via inheritance of technical and business exception base classes to allow reuse. However, if you want to have it this way, create such classes on your own derived from this class.
      Returns:
      true if this is a technical exception, false if this is a user error.
      See Also:
    • isForUser

      public boolean isForUser()
      Note: Please consider using net.sf.mmm.nls.exception.NlsException with i18n support in case you are creating exceptions for end-users.
      Returns:
      true if the message of this exception is for end-users (or clients), false otherwise (for internal technical errors).
    • getCode

      public String getCode()
      This method gets the code that identifies the detailed type of this object. While UUID is unique per instance of a ApplicationException this code is a short and readable identifier representing the ApplicationException Class. The default implementation returns the simple name. However, the code should remain stable after refactoring (so at least after the rename the previous code should be returned as String literal). This code may be used as a compact identifier to reference the related problem or information as well as for automatic tests of error situations that should remain stable even if the message text gets improved or the locale is unknown.
      Returns:
      the error code.
    • toString

      public String toString()
      Overrides:
      toString in class Throwable
    • toString

      public String toString(Locale locale)
      Like toString() but using the specified Locale.
      Parameters:
      locale - is the Locale used for getLocalizedMessage(Locale).
      Returns:
      the localized string representation of this exception as described in toString(Locale, Appendable)
    • toString

      public Appendable toString(Locale locale, Appendable appendable)
      appends the localized string representation of this exception. It is defined as following:
       «classname»: [«custom-code»: ]«message»
       
      Parameters:
      locale - is the Locale used for getLocalizedMessage(Locale).
      appendable - is the buffer to append to. Will be created as StringBuilder if null is provided.
      Returns:
      the provided Appendable or the created one if null was given.
    • getNlsMessage

      public static Localizable getNlsMessage(Throwable error)
      Parameters:
      error - the Throwable.
      Returns:
      the NLS message.