org.openbp.common
Class ExceptionUtil

java.lang.Object
  extended by org.openbp.common.ExceptionUtil

public final class ExceptionUtil
extends java.lang.Object

Static exception utiltity methods. The methods of this class can be used to obtain exception messages and stack traces. The account especially for nested exceptions, i. e. exceptions that are held not only as exception cause (see Throwable.getCause()), but may also be returned by one of the following methods, which will be accessed using reflections:
getException
getTargetException
getNestedException

Author:
Heiko Erhardt

Method Summary
static java.lang.RuntimeException extractRuntimeException(java.lang.Throwable e)
          Extracts a runtime exception from the given exception.
static java.lang.Throwable findNestedException(java.lang.Throwable t, java.lang.String className)
          Recurses the exception chain down until the specified exception.
static java.lang.Throwable getNestedException(java.lang.Throwable t)
          Gets the target excpeption of a Throwable object or the object itself.
static java.util.List getNestedExceptions(java.lang.Throwable throwable)
          Gets a list containing the given throwable and all its nested exceptions.
static java.lang.String getNestedMessage(java.lang.Throwable t)
          Gets the exception message of the throwable plus any additional message of exceptions nested by this throwable.
static java.lang.String getNestedTrace(java.lang.Throwable throwable)
          Gets the stack trace of the throwable plus the stack traces of exceptions nested by this throwable.
static void printTrace(java.lang.String msg, java.lang.Throwable t)
          Prints a nested stack trace of the given throwable to stderr.
static void printTrace(java.lang.Throwable t)
          Prints a nested stack trace of the given throwable to stderr.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getNestedMessage

public static java.lang.String getNestedMessage(java.lang.Throwable t)
Gets the exception message of the throwable plus any additional message of exceptions nested by this throwable. The method will consider not only the exception cause when printing nested exceptions, but also exceptions encapsulated by the methods mentioned in the comment of this class.
The method ensures that the returned string does not contain duplicate messages.

Parameters:
t - Throwable object
Returns:
The message or null if there is none

getNestedTrace

public static java.lang.String getNestedTrace(java.lang.Throwable throwable)
Gets the stack trace of the throwable plus the stack traces of exceptions nested by this throwable. This looks exactly like the output of the toString method of a Throwable, however, it does not only consider the exception cause when printing nested exceptions, but also exceptions encapsulated by the methods mentioned in the comment of this class.

Parameters:
throwable - Throwable object
Returns:
The message or null if there is none

getNestedExceptions

public static java.util.List getNestedExceptions(java.lang.Throwable throwable)
Gets a list containing the given throwable and all its nested exceptions.

Parameters:
throwable - Throwable object
Returns:
A list of Throwable objects.
The list will contain each exception in the exception chain only one.

getNestedException

public static java.lang.Throwable getNestedException(java.lang.Throwable t)
Gets the target excpeption of a Throwable object or the object itself.

Parameters:
t - Throwable object
Returns:
The last exception of the chain of target exceptions if the Throwable contains a nested exception (i. e. a method named getException(), getTargetException () or getNestedException() or the Throwable object itself otherwise

findNestedException

public static java.lang.Throwable findNestedException(java.lang.Throwable t,
                                                      java.lang.String className)
Recurses the exception chain down until the specified exception. This method can be used to determine if a particular exception is part of the exception chain.

Parameters:
t - Throwable object
className - Fully qualified class name of the exception to search
Returns:
The specified exception or null if the exception chain does not contain such an exception.

printTrace

public static void printTrace(java.lang.Throwable t)
Prints a nested stack trace of the given throwable to stderr.

Parameters:
t - Exception to print

printTrace

public static void printTrace(java.lang.String msg,
                              java.lang.Throwable t)
Prints a nested stack trace of the given throwable to stderr.

Parameters:
msg - Additional explaining message that should preceede the stack trace or null
t - Exception to print

extractRuntimeException

public static java.lang.RuntimeException extractRuntimeException(java.lang.Throwable e)
Extracts a runtime exception from the given exception. If the exception or it's direct cause is not a runtime exception, it will be wrapped into a new RuntimeException.

Parameters:
e - Exception to inspect
Returns:
The runtime exception


Copyright © 2011. All Rights Reserved.