Code with Finding: |
class ExceptionUtils {
/**
* <p>Introspects the <code>Throwable</code> to obtain the root cause.</p>
*
* <p>This method walks through the exception chain to the last element,
* "root" of the tree, using {@link #getCause(Throwable)}, and
* returns that exception.</p>
*
* <p>From version 2.2, this method handles recursive cause structures
* that might otherwise cause infinite loops. If the throwable parameter
* has a cause of itself, then null will be returned. If the throwable
* parameter cause chain loops, the last element in the chain before the
* loop is returned.</p>
*
* @param throwable the throwable to get the root cause for, may be null
* @return the root cause of the <code>Throwable</code>,
* <code>null</code> if none found or null throwable input
*/
public static Throwable getRootCause(Throwable throwable) {
List list = getThrowableList(throwable);
return (list.size() < 2 ? null : (Throwable)list.get(list.size() - 1));
}
}
|