|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException org.apache.commons.lang3.exception.ContextedRuntimeException
public class ContextedRuntimeException
A runtime exception that provides an easy and safe way to add contextual information.
An exception trace itself is often insufficient to provide rapid diagnosis of the issue. Frequently what is needed is a select few pieces of local contextual data. Providing this data is tricky however, due to concerns over formatting and nulls.
The contexted exception approach allows the exception to be created together with a map of context values. This additional information is automatically included in the message and printed stack trace.
An checked version of this exception is provided by ContextedException.
To use this class write code as follows:
try { ... } catch (Exception e) { throw new ContextedException("Error posting account transaction", e) .addValue("accountNumber", accountNumber) .addValue("amountPosted", amountPosted) .addValue("previousBalance", previousBalance) } }
The output in a printStacktrace() (which often is written to a log) would look something like the following:
org.apache.commons.lang3.exception.ContextedRuntimeException: java.lang.Exception: Error posting account transaction Exception Context: [accountNumber=null] [amountPosted=100.00] [previousBalance=-2.17] --------------------------------- at org.apache.commons.lang3.exception.ContextedRuntimeExceptionTest.testAddValue(ContextedExceptionTest.java:88) ..... (rest of trace)
ContextedException
,
Serialized FormConstructor Summary | |
---|---|
ContextedRuntimeException()
Instantiates ContextedRuntimeException without message or cause. |
|
ContextedRuntimeException(String message)
Instantiates ContextedRuntimeException with message, but without cause. |
|
ContextedRuntimeException(String message,
Throwable cause)
Instantiates ContextedRuntimeException with cause and message. |
|
ContextedRuntimeException(String message,
Throwable cause,
ExceptionContext context)
Instantiates ContextedRuntimeException with cause, message, and ExceptionContext. |
|
ContextedRuntimeException(Throwable cause)
Instantiates ContextedRuntimeException with cause, but without message. |
Method Summary | |
---|---|
ContextedRuntimeException |
addValue(String label,
Object value)
Adds information helpful to a developer in diagnosing and correcting the problem. |
String |
getFormattedExceptionMessage(String baseMessage)
Implementors provide the given base message with context label/value item information appended. |
Set<String> |
getLabelSet()
Retrieves the labels defined in the contextual data. |
String |
getMessage()
Provides the message explaining the exception, including the contextual data. |
Object |
getValue(String label)
Retrieves a contextual data value associated with the label. |
ContextedRuntimeException |
replaceValue(String label,
Object value)
Replaces information helpful to a developer in diagnosing and correcting the problem. |
Methods inherited from class java.lang.Throwable |
---|
fillInStackTrace, getCause, getLocalizedMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public ContextedRuntimeException()
The context information is stored using a default implementation.
public ContextedRuntimeException(String message)
The context information is stored using a default implementation.
message
- the exception message, may be nullpublic ContextedRuntimeException(Throwable cause)
The context information is stored using a default implementation.
cause
- the underlying cause of the exception, may be nullpublic ContextedRuntimeException(String message, Throwable cause)
The context information is stored using a default implementation.
message
- the exception message, may be nullcause
- the underlying cause of the exception, may be nullpublic ContextedRuntimeException(String message, Throwable cause, ExceptionContext context)
message
- the exception message, may be nullcause
- the underlying cause of the exception, may be nullcontext
- the context used to store the additional information, null uses default implementationMethod Detail |
---|
public ContextedRuntimeException addValue(String label, Object value)
Note: This exception is only serializable if the object added is serializable.
addValue
in interface ExceptionContext
label
- a textual label associated with information, null not recommendedvalue
- information needed to understand exception, may be null
public ContextedRuntimeException replaceValue(String label, Object value)
Note: This exception is only serializable if the object added is serializable.
replaceValue
in interface ExceptionContext
label
- a textual label associated with information, null not recommendedvalue
- information needed to understand exception, may be null
public Object getValue(String label)
getValue
in interface ExceptionContext
label
- the label to get the contextual value for, may be null
public Set<String> getLabelSet()
getLabelSet
in interface ExceptionContext
public String getMessage()
getMessage
in class Throwable
Throwable.getMessage()
public String getFormattedExceptionMessage(String baseMessage)
getFormattedExceptionMessage
in interface ExceptionContext
baseMessage
- the base exception message without context information appended
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |