Top Description Fields Constructors Methods
org.apache.commons.lang3.exception

public Class ContextedRuntimeException

extends RuntimeException
implements ExceptionContext
Class Inheritance
All Implemented Interfaces
org.apache.commons.lang3.exception.ExceptionContext
Imports
java.util.List, .Set, org.apache.commons.lang3.tuple.Pair

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 list of context label-value pairs. This additional information is automatically included in the message and printed stack trace.

A checked version of this exception is provided by ContextedException.

To use this class write code as follows:

  try {
    ...
  } catch (Exception e) {
    throw new ContextedRuntimeException("Error posting account transaction", e)
         .addContextValue("Account Number", accountNumber)
         .addContextValue("Amount Posted", amountPosted)
         .addContextValue("Previous Balance", previousBalance);
  }
}

or improve diagnose data at a higher level:

  try {
    ...
  } catch (ContextedRuntimeException e) {
    throw e.setContextValue("Transaction Id", transactionId);
  } catch (Exception e) {
    if (e instanceof ExceptionContext) {
      e.setContextValue("Transaction Id", transactionId);
    }
    throw e;
  }
}

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:
 [1:Account Number=null]
 [2:Amount Posted=100.00]
 [3:Previous Balance=-2.17]
 [4:Transaction Id=94ef1d15-d443-46c4-822b-637f26244899]

 ---------------------------------
 at org.apache.commons.lang3.exception.ContextedRuntimeExceptionTest.testAddValue(ContextedExceptionTest.java:88)
 ..... (rest of trace)
Since
3.0
See Also
ContextedException

Field Summary

Modifier and TypeField and Description
private final ExceptionContext
exceptionContext

The context where the data is stored.

private static final long

Constructor Summary

AccessConstructor and Description
public
ContextedRuntimeException()

Instantiates ContextedRuntimeException without message or cause.

public
ContextedRuntimeException(final String
the exception message, may be null
message
)

Instantiates ContextedRuntimeException with message, but without cause.

public
ContextedRuntimeException(final String
the exception message, may be null
message
,
final Throwable
the underlying cause of the exception, may be null
cause
)

Instantiates ContextedRuntimeException with cause and message.

public
ContextedRuntimeException(final String
the exception message, may be null
message
,
final Throwable
the underlying cause of the exception, may be null
cause
,
ExceptionContext
the context used to store the additional information, null uses default implementation
context
)

Instantiates ContextedRuntimeException with cause, message, and ExceptionContext.

public
ContextedRuntimeException(final Throwable
the underlying cause of the exception, may be null
cause
)

Instantiates ContextedRuntimeException with cause, but without message.

Method Summary

Modifier and TypeMethod and Description
public ContextedRuntimeException

Returns:

this, for method chaining, not null
addContextValue
(final String
a textual label associated with information, null not recommended
label
,
final Object
information needed to understand exception, may be null
value
)

Implements org.apache.commons.lang3.exception.ExceptionContext.addContextValue.

Adds information helpful to a developer in diagnosing and correcting the problem.

public List<Pair<String, Object>>
getContextEntries()

Implements org.apache.commons.lang3.exception.ExceptionContext.getContextEntries.

Gets the full list of label-value pairs defined in the contextual data.

public Set<String>
getContextLabels()

Implements org.apache.commons.lang3.exception.ExceptionContext.getContextLabels.

Gets the full set of labels defined in the contextual data.

public List<Object>
getContextValues(final String label)

Implements org.apache.commons.lang3.exception.ExceptionContext.getContextValues.

Gets all the contextual data values associated with the label.

public Object
getFirstContextValue(final String label)

Implements org.apache.commons.lang3.exception.ExceptionContext.getFirstContextValue.

Gets the first available contextual data value associated with the label.

public String
getFormattedExceptionMessage(final String baseMessage)

Implements org.apache.commons.lang3.exception.ExceptionContext.getFormattedExceptionMessage.

Gets the contextualized error message based on a base message.

public String

Returns:

the message, never null
getMessage
()

Overrides java.lang.Throwable.getMessage.

Provides the message explaining the exception, including the contextual data.

public String

Returns:

the message
getRawMessage
()

Provides the message explaining the exception without the contextual data.

public ContextedRuntimeException

Returns:

this, for method chaining, not null
setContextValue
(final String
a textual label associated with information, null not recommended
label
,
final Object
information needed to understand exception, may be null
value
)

Implements org.apache.commons.lang3.exception.ExceptionContext.setContextValue.

Sets information helpful to a developer in diagnosing and correcting the problem.

Field Detail

exceptionContextback to summary
private final ExceptionContext exceptionContext

The context where the data is stored.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.RuntimeException.serialVersionUID.

The serialization version.

Constructor Detail

ContextedRuntimeExceptionback to summary
public ContextedRuntimeException()

Instantiates ContextedRuntimeException without message or cause.

The context information is stored using a default implementation.

ContextedRuntimeExceptionback to summary
public ContextedRuntimeException(final String message)

Instantiates ContextedRuntimeException with message, but without cause.

The context information is stored using a default implementation.

Parameters
message:String

the exception message, may be null

ContextedRuntimeExceptionback to summary
public ContextedRuntimeException(final String message, final Throwable cause)

Instantiates ContextedRuntimeException with cause and message.

The context information is stored using a default implementation.

Parameters
message:String

the exception message, may be null

cause:Throwable

the underlying cause of the exception, may be null

ContextedRuntimeExceptionback to summary
public ContextedRuntimeException(final String message, final Throwable cause, ExceptionContext context)

Instantiates ContextedRuntimeException with cause, message, and ExceptionContext.

Parameters
message:String

the exception message, may be null

cause:Throwable

the underlying cause of the exception, may be null

context:ExceptionContext

the context used to store the additional information, null uses default implementation

ContextedRuntimeExceptionback to summary
public ContextedRuntimeException(final Throwable cause)

Instantiates ContextedRuntimeException with cause, but without message.

The context information is stored using a default implementation.

Parameters
cause:Throwable

the underlying cause of the exception, may be null

Method Detail

addContextValueback to summary
public ContextedRuntimeException addContextValue(final String label, final Object value)

Implements org.apache.commons.lang3.exception.ExceptionContext.addContextValue.

Adds information helpful to a developer in diagnosing and correcting the problem. For the information to be meaningful, the value passed should have a reasonable toString() implementation. Different values can be added with the same label multiple times.

Note

This exception is only serializable if the object added is serializable.

Parameters
label:String

a textual label associated with information, null not recommended

value:Object

information needed to understand exception, may be null

Returns:ContextedRuntimeException

this, for method chaining, not null

Annotations
@Override
getContextEntriesback to summary
public List<Pair<String, Object>> getContextEntries()

Implements org.apache.commons.lang3.exception.ExceptionContext.getContextEntries.

Doc from org.apache.commons.lang3.exception.ExceptionContext.getContextEntries.

Gets the full list of label-value pairs defined in the contextual data.

Annotations
@Override
getContextLabelsback to summary
public Set<String> getContextLabels()

Implements org.apache.commons.lang3.exception.ExceptionContext.getContextLabels.

Doc from org.apache.commons.lang3.exception.ExceptionContext.getContextLabels.

Gets the full set of labels defined in the contextual data.

Annotations
@Override
getContextValuesback to summary
public List<Object> getContextValues(final String label)

Implements org.apache.commons.lang3.exception.ExceptionContext.getContextValues.

Doc from org.apache.commons.lang3.exception.ExceptionContext.getContextValues.

Gets all the contextual data values associated with the label.

Annotations
@Override
getFirstContextValueback to summary
public Object getFirstContextValue(final String label)

Implements org.apache.commons.lang3.exception.ExceptionContext.getFirstContextValue.

Doc from org.apache.commons.lang3.exception.ExceptionContext.getFirstContextValue.

Gets the first available contextual data value associated with the label.

Annotations
@Override
getFormattedExceptionMessageback to summary
public String getFormattedExceptionMessage(final String baseMessage)

Implements org.apache.commons.lang3.exception.ExceptionContext.getFormattedExceptionMessage.

Doc from org.apache.commons.lang3.exception.ExceptionContext.getFormattedExceptionMessage.

Gets the contextualized error message based on a base message. This will add the context label-value pairs to the message.

Annotations
@Override
getMessageback to summary
public String getMessage()

Overrides java.lang.Throwable.getMessage.

Provides the message explaining the exception, including the contextual data.

Returns:String

the message, never null

Annotations
@Override
See Also
Throwable#getMessage()
getRawMessageback to summary
public String getRawMessage()

Provides the message explaining the exception without the contextual data.

Returns:String

the message

Since
3.0.1
See Also
Throwable#getMessage()
setContextValueback to summary
public ContextedRuntimeException setContextValue(final String label, final Object value)

Implements org.apache.commons.lang3.exception.ExceptionContext.setContextValue.

Sets information helpful to a developer in diagnosing and correcting the problem. For the information to be meaningful, the value passed should have a reasonable toString() implementation. Any existing values with the same labels are removed before the new one is added.

Note

This exception is only serializable if the object added as value is serializable.

Parameters
label:String

a textual label associated with information, null not recommended

value:Object

information needed to understand exception, may be null

Returns:ContextedRuntimeException

this, for method chaining, not null

Annotations
@Override