/** * <p>Counts the number of <code>Throwable</code> objects in the * exception chain.</p> * * <p>A throwable without cause will return <code>1</code>. * A throwable with one cause will return <code>2</code> and so on. * A <code>null</code> throwable will return <code>0</code>.</p> * * <p>From version 2.2, this method handles recursive cause structures * that might otherwise cause infinite loops. The cause chain is * processed until the end is reached, or until the next item in the * chain is already in the result set.</p> * * @param throwable the throwable to inspect, may be null * @return the count of throwables, zero if null input */ public static int getThrowableCount(Throwable throwable) { return getThrowableList(throwable).size(); }
/** * <p>Returns the list of <code>Throwable</code> objects in the * exception chain.</p> * * <p>A throwable without cause will return an array containing * one element - the input throwable. * A throwable with one cause will return an array containing * two elements. - the input throwable and the cause throwable. * A <code>null</code> throwable will return an array of size zero.</p> * * <p>From version 2.2, this method handles recursive cause structures * that might otherwise cause infinite loops. The cause chain is * processed until the end is reached, or until the next item in the * chain is already in the result set.</p> * * @see #getThrowableList(Throwable) * @param throwable the throwable to inspect, may be null * @return the array of throwables, never null */ public static Throwable[] getThrowables(Throwable throwable) { List list = getThrowableList(throwable); return (Throwable[]) list.toArray(new Throwable[list.size()]); }
/** * <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)); }