- 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:
CliException
,DuplicateObjectException
,ObjectMismatchException
,ObjectNotFoundException
,ReadOnlyException
,RuntimeIoException
,TechnicalErrorUserException
public abstract class ApplicationException extends RuntimeException implements Localizable
ExtendsRuntimeException
with the following features:- a
UUID
unique per exception instance automatically generated once per exceptionchain
. - an
error code
that should be unique per exception type. - distinction between
technical
exceptions and exceptionsintended for end-users
.
Exceptions should only occur in unexpected or undesired situations. Never use exceptions for control flows.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Modifier Constructor Description ApplicationException(Localizable message)
The constructor.ApplicationException(Localizable message, Throwable cause)
The constructor.protected
ApplicationException(Localizable message, Throwable cause, UUID uuid)
The constructor.ApplicationException(String message)
The constructor.ApplicationException(String message, Throwable cause)
The constructor.protected
ApplicationException(String message, Throwable cause, UUID uuid)
The constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected UUID
createUuid()
String
getCode()
This method gets the code that identifies the detailed type of this object.String
getLocalizedMessage(Locale locale)
This method gets the resolved and localized message.void
getLocalizedMessage(Locale locale, Appendable appendable)
This method writes thelocalized message
to the givenAppendable
.String
getMessage()
ATTENTION:
In most cases you wand to useLocalizable.getLocalizedMessage(Locale)
instead of this method.Localizable
getNlsMessage()
static Localizable
getNlsMessage(Throwable error)
UUID
getUuid()
This method gets theUUID
of thisApplicationException
.boolean
isForUser()
Note: Please consider usingnet.sf.mmm.nls.exception.NlsException
with i18n support in case you are creating exceptions for end-users.boolean
isTechnical()
Determines if this is a technical exception.void
printStackTrace(Locale locale, Appendable buffer)
This method prints the stack trace with localized exception message(s).String
toString()
String
toString(Locale locale)
LiketoString()
but using the specifiedLocale
.Appendable
toString(Locale locale, Appendable appendable)
appends
the localized string representation of this exception.-
Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.github.mmm.base.i18n.Localizable
getArgument, getLocalizedMessage, toLocalizable
-
-
-
-
Constructor Detail
-
ApplicationException
public ApplicationException(String message)
The constructor.- Parameters:
message
- themessage
describing the problem briefly.
-
ApplicationException
public ApplicationException(Localizable message)
The constructor.- Parameters:
message
- themessage
describing the problem briefly.
-
ApplicationException
public ApplicationException(Localizable message, Throwable cause)
The constructor.
-
ApplicationException
protected ApplicationException(String message, Throwable cause, UUID uuid)
The constructor.
-
ApplicationException
protected ApplicationException(Localizable message, Throwable cause, UUID uuid)
The constructor.
-
-
Method Detail
-
getUuid
public final UUID getUuid()
This method gets theUUID
of thisApplicationException
. When a newApplicationException
is created, aUUID
is assigned. In case theApplicationException
is created from anotherApplicationException
as cause, the existingUUID
will be used from that cause. Otherwise a newUUID
is generated.
TheUUID
will appear in thestacktrace
but NOT in themessage
. It will therefore be written to log-files if this exception is logged. If you supply theUUID
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()
Description copied from interface:Localizable
ATTENTION:
In most cases you wand to useLocalizable.getLocalizedMessage(Locale)
instead of this method.- Specified by:
getMessage
in interfaceLocalizable
- Overrides:
getMessage
in classThrowable
- Returns:
- the untranslated message with arguments filled in. This results in the message in its original language that should typically be English.
- See Also:
Localizable.getLocalizedMessage()
-
getNlsMessage
public Localizable getNlsMessage()
- Returns:
- the
Localizable
message describing the problem. - See Also:
getMessage()
,getLocalizedMessage(Locale)
-
getLocalizedMessage
public String getLocalizedMessage(Locale locale)
Description copied from interface:Localizable
This method gets the resolved and localized message.- Specified by:
getLocalizedMessage
in interfaceLocalizable
- 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
- Specified by:
getLocalizedMessage
in interfaceLocalizable
- Parameters:
locale
- theLocale
to translate to.appendable
- theAppendable
where towrite
the message to.- See Also:
Localizable.getLocalizedMessage(Locale)
-
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 givenbuffer
produced anIOException
.
-
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 typicallyintended for to end-users
and has to be easy to understand.
- Returns:
true
if this is a technical exception,false
if this is a user error.- See Also:
isForUser()
- 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
-
isForUser
public boolean isForUser()
Note: Please consider usingnet.sf.mmm.nls.exception.NlsException
with i18n support in case you are creating exceptions for end-users.
-
getCode
public String getCode()
This method gets the code that identifies the detailed type of this object. WhileUUID
is unique per instance of aApplicationException
this code is a short and readable identifier representing theApplicationException
Class
. The default implementation returns thesimple name
. However, the code should remain stable after refactoring (so at least after the rename the previous code should be returned asString
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(Locale locale)
LiketoString()
but using the specifiedLocale
.- Parameters:
locale
- is theLocale
used forgetLocalizedMessage(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 theLocale
used forgetLocalizedMessage(Locale)
.appendable
- is the buffer toappend
to. Will be created asStringBuilder
ifnull
is provided.- Returns:
- the provided
Appendable
or the created one ifnull
was given.
-
getNlsMessage
public static Localizable getNlsMessage(Throwable error)
- Parameters:
error
- theThrowable
.- Returns:
- the
NLS message
.
-
-