View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.exception;
18  
19  import java.io.PrintStream;
20  import java.io.PrintWriter;
21  import java.io.StringWriter;
22  import java.lang.reflect.InvocationTargetException;
23  import java.lang.reflect.Method;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.StringTokenizer;
27  
28  import org.apache.commons.lang3.ArrayUtils;
29  import org.apache.commons.lang3.ClassUtils;
30  import org.apache.commons.lang3.StringUtils;
31  import org.apache.commons.lang3.SystemUtils;
32  
33  /**
34   * <p>Provides utilities for manipulating and examining 
35   * <code>Throwable</code> objects.</p>
36   *
37   * @since 1.0
38   */
39  public class ExceptionUtils {
40      
41      /**
42       * <p>Used when printing stack frames to denote the start of a
43       * wrapped exception.</p>
44       *
45       * <p>Package private for accessibility by test suite.</p>
46       */
47      static final String WRAPPED_MARKER = " [wrapped] ";
48  
49      /**
50       * <p>The names of methods commonly used to access a wrapped exception.</p>
51       */
52      // TODO: Remove in Lang 4.0
53      private static final String[] CAUSE_METHOD_NAMES = {
54          "getCause",
55          "getNextException",
56          "getTargetException",
57          "getException",
58          "getSourceException",
59          "getRootCause",
60          "getCausedByException",
61          "getNested",
62          "getLinkedException",
63          "getNestedException",
64          "getLinkedCause",
65          "getThrowable",
66      };
67  
68      /**
69       * <p>
70       * Public constructor allows an instance of <code>ExceptionUtils</code> to be created, although that is not
71       * normally necessary.
72       * </p>
73       */
74      public ExceptionUtils() {
75          super();
76      }
77  
78      //-----------------------------------------------------------------------
79      /**
80       * <p>Returns the default names used when searching for the cause of an exception.</p>
81       *
82       * <p>This may be modified and used in the overloaded getCause(Throwable, String[]) method.</p>
83       *
84       * @return cloned array of the default method names
85       * @since 3.0
86       * @deprecated This feature will be removed in Lang 4.0
87       */
88      @Deprecated
89      public static String[] getDefaultCauseMethodNames() {
90          return ArrayUtils.clone(CAUSE_METHOD_NAMES);
91      }
92  
93      //-----------------------------------------------------------------------
94      /**
95       * <p>Introspects the <code>Throwable</code> to obtain the cause.</p>
96       *
97       * <p>The method searches for methods with specific names that return a 
98       * <code>Throwable</code> object. This will pick up most wrapping exceptions,
99       * including those from JDK 1.4.
100      *
101      * <p>The default list searched for are:</p>
102      * <ul>
103      *  <li><code>getCause()</code></li>
104      *  <li><code>getNextException()</code></li>
105      *  <li><code>getTargetException()</code></li>
106      *  <li><code>getException()</code></li>
107      *  <li><code>getSourceException()</code></li>
108      *  <li><code>getRootCause()</code></li>
109      *  <li><code>getCausedByException()</code></li>
110      *  <li><code>getNested()</code></li>
111      * </ul>
112      * 
113      * <p>If none of the above is found, returns <code>null</code>.</p>
114      *
115      * @param throwable  the throwable to introspect for a cause, may be null
116      * @return the cause of the <code>Throwable</code>,
117      *  <code>null</code> if none found or null throwable input
118      * @since 1.0
119      * @deprecated This feature will be removed in Lang 4.0
120      */
121     @Deprecated
122     public static Throwable getCause(final Throwable throwable) {
123         return getCause(throwable, CAUSE_METHOD_NAMES);
124     }
125 
126     /**
127      * <p>Introspects the <code>Throwable</code> to obtain the cause.</p>
128      *
129      * <p>A <code>null</code> set of method names means use the default set.
130      * A <code>null</code> in the set of method names will be ignored.</p>
131      *
132      * @param throwable  the throwable to introspect for a cause, may be null
133      * @param methodNames  the method names, null treated as default set
134      * @return the cause of the <code>Throwable</code>,
135      *  <code>null</code> if none found or null throwable input
136      * @since 1.0
137      * @deprecated This feature will be removed in Lang 4.0
138      */
139     @Deprecated
140     public static Throwable getCause(final Throwable throwable, String[] methodNames) {
141         if (throwable == null) {
142             return null;
143         }
144 
145         if (methodNames == null) {
146             methodNames = CAUSE_METHOD_NAMES;
147         }
148 
149         for (final String methodName : methodNames) {
150             if (methodName != null) {
151                 final Throwable cause = getCauseUsingMethodName(throwable, methodName);
152                 if (cause != null) {
153                     return cause;
154                 }
155             }
156         }
157 
158         return null;
159     }
160 
161     /**
162      * <p>Introspects the <code>Throwable</code> to obtain the root cause.</p>
163      *
164      * <p>This method walks through the exception chain to the last element,
165      * "root" of the tree, using {@link #getCause(Throwable)}, and
166      * returns that exception.</p>
167      *
168      * <p>From version 2.2, this method handles recursive cause structures
169      * that might otherwise cause infinite loops. If the throwable parameter
170      * has a cause of itself, then null will be returned. If the throwable
171      * parameter cause chain loops, the last element in the chain before the
172      * loop is returned.</p>
173      *
174      * @param throwable  the throwable to get the root cause for, may be null
175      * @return the root cause of the <code>Throwable</code>,
176      *  <code>null</code> if none found or null throwable input
177      */
178     public static Throwable getRootCause(final Throwable throwable) {
179         final List<Throwable> list = getThrowableList(throwable);
180         return list.size() < 2 ? null : (Throwable)list.get(list.size() - 1);
181     }
182 
183     /**
184      * <p>Finds a <code>Throwable</code> by method name.</p>
185      *
186      * @param throwable  the exception to examine
187      * @param methodName  the name of the method to find and invoke
188      * @return the wrapped exception, or <code>null</code> if not found
189      */
190     // TODO: Remove in Lang 4.0
191     private static Throwable getCauseUsingMethodName(final Throwable throwable, final String methodName) {
192         Method method = null;
193         try {
194             method = throwable.getClass().getMethod(methodName);
195         } catch (final NoSuchMethodException ignored) { // NOPMD
196             // exception ignored
197         } catch (final SecurityException ignored) { // NOPMD
198             // exception ignored
199         }
200 
201         if (method != null && Throwable.class.isAssignableFrom(method.getReturnType())) {
202             try {
203                 return (Throwable) method.invoke(throwable);
204             } catch (final IllegalAccessException ignored) { // NOPMD
205                 // exception ignored
206             } catch (final IllegalArgumentException ignored) { // NOPMD
207                 // exception ignored
208             } catch (final InvocationTargetException ignored) { // NOPMD
209                 // exception ignored
210             }
211         }
212         return null;
213     }
214 
215     //-----------------------------------------------------------------------
216     /**
217      * <p>Counts the number of <code>Throwable</code> objects in the
218      * exception chain.</p>
219      *
220      * <p>A throwable without cause will return <code>1</code>.
221      * A throwable with one cause will return <code>2</code> and so on.
222      * A <code>null</code> throwable will return <code>0</code>.</p>
223      *
224      * <p>From version 2.2, this method handles recursive cause structures
225      * that might otherwise cause infinite loops. The cause chain is
226      * processed until the end is reached, or until the next item in the
227      * chain is already in the result set.</p>
228      *
229      * @param throwable  the throwable to inspect, may be null
230      * @return the count of throwables, zero if null input
231      */
232     public static int getThrowableCount(final Throwable throwable) {
233         return getThrowableList(throwable).size();
234     }
235 
236     /**
237      * <p>Returns the list of <code>Throwable</code> objects in the
238      * exception chain.</p>
239      *
240      * <p>A throwable without cause will return an array containing
241      * one element - the input throwable.
242      * A throwable with one cause will return an array containing
243      * two elements. - the input throwable and the cause throwable.
244      * A <code>null</code> throwable will return an array of size zero.</p>
245      *
246      * <p>From version 2.2, this method handles recursive cause structures
247      * that might otherwise cause infinite loops. The cause chain is
248      * processed until the end is reached, or until the next item in the
249      * chain is already in the result set.</p>
250      *
251      * @see #getThrowableList(Throwable)
252      * @param throwable  the throwable to inspect, may be null
253      * @return the array of throwables, never null
254      */
255     public static Throwable[] getThrowables(final Throwable throwable) {
256         final List<Throwable> list = getThrowableList(throwable);
257         return list.toArray(new Throwable[list.size()]);
258     }
259 
260     /**
261      * <p>Returns the list of <code>Throwable</code> objects in the
262      * exception chain.</p>
263      *
264      * <p>A throwable without cause will return a list containing
265      * one element - the input throwable.
266      * A throwable with one cause will return a list containing
267      * two elements. - the input throwable and the cause throwable.
268      * A <code>null</code> throwable will return a list of size zero.</p>
269      *
270      * <p>This method handles recursive cause structures that might
271      * otherwise cause infinite loops. The cause chain is processed until
272      * the end is reached, or until the next item in the chain is already
273      * in the result set.</p>
274      *
275      * @param throwable  the throwable to inspect, may be null
276      * @return the list of throwables, never null
277      * @since Commons Lang 2.2
278      */
279     public static List<Throwable> getThrowableList(Throwable throwable) {
280         final List<Throwable> list = new ArrayList<Throwable>();
281         while (throwable != null && list.contains(throwable) == false) {
282             list.add(throwable);
283             throwable = ExceptionUtils.getCause(throwable);
284         }
285         return list;
286     }
287 
288     //-----------------------------------------------------------------------
289     /**
290      * <p>Returns the (zero based) index of the first <code>Throwable</code>
291      * that matches the specified class (exactly) in the exception chain.
292      * Subclasses of the specified class do not match - see
293      * {@link #indexOfType(Throwable, Class)} for the opposite.</p>
294      *
295      * <p>A <code>null</code> throwable returns <code>-1</code>.
296      * A <code>null</code> type returns <code>-1</code>.
297      * No match in the chain returns <code>-1</code>.</p>
298      *
299      * @param throwable  the throwable to inspect, may be null
300      * @param clazz  the class to search for, subclasses do not match, null returns -1
301      * @return the index into the throwable chain, -1 if no match or null input
302      */
303     public static int indexOfThrowable(final Throwable throwable, final Class<?> clazz) {
304         return indexOf(throwable, clazz, 0, false);
305     }
306 
307     /**
308      * <p>Returns the (zero based) index of the first <code>Throwable</code>
309      * that matches the specified type in the exception chain from
310      * a specified index.
311      * Subclasses of the specified class do not match - see
312      * {@link #indexOfType(Throwable, Class, int)} for the opposite.</p>
313      *
314      * <p>A <code>null</code> throwable returns <code>-1</code>.
315      * A <code>null</code> type returns <code>-1</code>.
316      * No match in the chain returns <code>-1</code>.
317      * A negative start index is treated as zero.
318      * A start index greater than the number of throwables returns <code>-1</code>.</p>
319      *
320      * @param throwable  the throwable to inspect, may be null
321      * @param clazz  the class to search for, subclasses do not match, null returns -1
322      * @param fromIndex  the (zero based) index of the starting position,
323      *  negative treated as zero, larger than chain size returns -1
324      * @return the index into the throwable chain, -1 if no match or null input
325      */
326     public static int indexOfThrowable(final Throwable throwable, final Class<?> clazz, final int fromIndex) {
327         return indexOf(throwable, clazz, fromIndex, false);
328     }
329 
330     //-----------------------------------------------------------------------
331     /**
332      * <p>Returns the (zero based) index of the first <code>Throwable</code>
333      * that matches the specified class or subclass in the exception chain.
334      * Subclasses of the specified class do match - see
335      * {@link #indexOfThrowable(Throwable, Class)} for the opposite.</p>
336      *
337      * <p>A <code>null</code> throwable returns <code>-1</code>.
338      * A <code>null</code> type returns <code>-1</code>.
339      * No match in the chain returns <code>-1</code>.</p>
340      *
341      * @param throwable  the throwable to inspect, may be null
342      * @param type  the type to search for, subclasses match, null returns -1
343      * @return the index into the throwable chain, -1 if no match or null input
344      * @since 2.1
345      */
346     public static int indexOfType(final Throwable throwable, final Class<?> type) {
347         return indexOf(throwable, type, 0, true);
348     }
349 
350     /**
351      * <p>Returns the (zero based) index of the first <code>Throwable</code>
352      * that matches the specified type in the exception chain from
353      * a specified index.
354      * Subclasses of the specified class do match - see
355      * {@link #indexOfThrowable(Throwable, Class)} for the opposite.</p>
356      *
357      * <p>A <code>null</code> throwable returns <code>-1</code>.
358      * A <code>null</code> type returns <code>-1</code>.
359      * No match in the chain returns <code>-1</code>.
360      * A negative start index is treated as zero.
361      * A start index greater than the number of throwables returns <code>-1</code>.</p>
362      *
363      * @param throwable  the throwable to inspect, may be null
364      * @param type  the type to search for, subclasses match, null returns -1
365      * @param fromIndex  the (zero based) index of the starting position,
366      *  negative treated as zero, larger than chain size returns -1
367      * @return the index into the throwable chain, -1 if no match or null input
368      * @since 2.1
369      */
370     public static int indexOfType(final Throwable throwable, final Class<?> type, final int fromIndex) {
371         return indexOf(throwable, type, fromIndex, true);
372     }
373 
374     /**
375      * <p>Worker method for the <code>indexOfType</code> methods.</p>
376      *
377      * @param throwable  the throwable to inspect, may be null
378      * @param type  the type to search for, subclasses match, null returns -1
379      * @param fromIndex  the (zero based) index of the starting position,
380      *  negative treated as zero, larger than chain size returns -1
381      * @param subclass if <code>true</code>, compares with {@link Class#isAssignableFrom(Class)}, otherwise compares
382      * using references
383      * @return index of the <code>type</code> within throwables nested within the specified <code>throwable</code>
384      */
385     private static int indexOf(final Throwable throwable, final Class<?> type, int fromIndex, final boolean subclass) {
386         if (throwable == null || type == null) {
387             return -1;
388         }
389         if (fromIndex < 0) {
390             fromIndex = 0;
391         }
392         final Throwable[] throwables = ExceptionUtils.getThrowables(throwable);
393         if (fromIndex >= throwables.length) {
394             return -1;
395         }
396         if (subclass) {
397             for (int i = fromIndex; i < throwables.length; i++) {
398                 if (type.isAssignableFrom(throwables[i].getClass())) {
399                     return i;
400                 }
401             }
402         } else {
403             for (int i = fromIndex; i < throwables.length; i++) {
404                 if (type.equals(throwables[i].getClass())) {
405                     return i;
406                 }
407             }
408         }
409         return -1;
410     }
411 
412     //-----------------------------------------------------------------------
413     /**
414      * <p>Prints a compact stack trace for the root cause of a throwable
415      * to <code>System.err</code>.</p>
416      *
417      * <p>The compact stack trace starts with the root cause and prints
418      * stack frames up to the place where it was caught and wrapped.
419      * Then it prints the wrapped exception and continues with stack frames
420      * until the wrapper exception is caught and wrapped again, etc.</p>
421      *
422      * <p>The output of this method is consistent across JDK versions.
423      * Note that this is the opposite order to the JDK1.4 display.</p>
424      *
425      * <p>The method is equivalent to <code>printStackTrace</code> for throwables
426      * that don't have nested causes.</p>
427      *
428      * @param throwable  the throwable to output
429      * @since 2.0
430      */
431     public static void printRootCauseStackTrace(final Throwable throwable) {
432         printRootCauseStackTrace(throwable, System.err);
433     }
434 
435     /**
436      * <p>Prints a compact stack trace for the root cause of a throwable.</p>
437      *
438      * <p>The compact stack trace starts with the root cause and prints
439      * stack frames up to the place where it was caught and wrapped.
440      * Then it prints the wrapped exception and continues with stack frames
441      * until the wrapper exception is caught and wrapped again, etc.</p>
442      *
443      * <p>The output of this method is consistent across JDK versions.
444      * Note that this is the opposite order to the JDK1.4 display.</p>
445      *
446      * <p>The method is equivalent to <code>printStackTrace</code> for throwables
447      * that don't have nested causes.</p>
448      *
449      * @param throwable  the throwable to output, may be null
450      * @param stream  the stream to output to, may not be null
451      * @throws IllegalArgumentException if the stream is <code>null</code>
452      * @since 2.0
453      */
454     public static void printRootCauseStackTrace(final Throwable throwable, final PrintStream stream) {
455         if (throwable == null) {
456             return;
457         }
458         if (stream == null) {
459             throw new IllegalArgumentException("The PrintStream must not be null");
460         }
461         final String trace[] = getRootCauseStackTrace(throwable);
462         for (final String element : trace) {
463             stream.println(element);
464         }
465         stream.flush();
466     }
467 
468     /**
469      * <p>Prints a compact stack trace for the root cause of a throwable.</p>
470      *
471      * <p>The compact stack trace starts with the root cause and prints
472      * stack frames up to the place where it was caught and wrapped.
473      * Then it prints the wrapped exception and continues with stack frames
474      * until the wrapper exception is caught and wrapped again, etc.</p>
475      *
476      * <p>The output of this method is consistent across JDK versions.
477      * Note that this is the opposite order to the JDK1.4 display.</p>
478      *
479      * <p>The method is equivalent to <code>printStackTrace</code> for throwables
480      * that don't have nested causes.</p>
481      *
482      * @param throwable  the throwable to output, may be null
483      * @param writer  the writer to output to, may not be null
484      * @throws IllegalArgumentException if the writer is <code>null</code>
485      * @since 2.0
486      */
487     public static void printRootCauseStackTrace(final Throwable throwable, final PrintWriter writer) {
488         if (throwable == null) {
489             return;
490         }
491         if (writer == null) {
492             throw new IllegalArgumentException("The PrintWriter must not be null");
493         }
494         final String trace[] = getRootCauseStackTrace(throwable);
495         for (final String element : trace) {
496             writer.println(element);
497         }
498         writer.flush();
499     }
500 
501     //-----------------------------------------------------------------------
502     /**
503      * <p>Creates a compact stack trace for the root cause of the supplied
504      * <code>Throwable</code>.</p>
505      *
506      * <p>The output of this method is consistent across JDK versions.
507      * It consists of the root exception followed by each of its wrapping
508      * exceptions separated by '[wrapped]'. Note that this is the opposite
509      * order to the JDK1.4 display.</p>
510      *
511      * @param throwable  the throwable to examine, may be null
512      * @return an array of stack trace frames, never null
513      * @since 2.0
514      */
515     public static String[] getRootCauseStackTrace(final Throwable throwable) {
516         if (throwable == null) {
517             return ArrayUtils.EMPTY_STRING_ARRAY;
518         }
519         final Throwable throwables[] = getThrowables(throwable);
520         final int count = throwables.length;
521         final List<String> frames = new ArrayList<String>();
522         List<String> nextTrace = getStackFrameList(throwables[count - 1]);
523         for (int i = count; --i >= 0;) {
524             final List<String> trace = nextTrace;
525             if (i != 0) {
526                 nextTrace = getStackFrameList(throwables[i - 1]);
527                 removeCommonFrames(trace, nextTrace);
528             }
529             if (i == count - 1) {
530                 frames.add(throwables[i].toString());
531             } else {
532                 frames.add(WRAPPED_MARKER + throwables[i].toString());
533             }
534             for (int j = 0; j < trace.size(); j++) {
535                 frames.add(trace.get(j));
536             }
537         }
538         return frames.toArray(new String[frames.size()]);
539     }
540 
541     /**
542      * <p>Removes common frames from the cause trace given the two stack traces.</p>
543      *
544      * @param causeFrames  stack trace of a cause throwable
545      * @param wrapperFrames  stack trace of a wrapper throwable
546      * @throws IllegalArgumentException if either argument is null
547      * @since 2.0
548      */
549     public static void removeCommonFrames(final List<String> causeFrames, final List<String> wrapperFrames) {
550         if (causeFrames == null || wrapperFrames == null) {
551             throw new IllegalArgumentException("The List must not be null");
552         }
553         int causeFrameIndex = causeFrames.size() - 1;
554         int wrapperFrameIndex = wrapperFrames.size() - 1;
555         while (causeFrameIndex >= 0 && wrapperFrameIndex >= 0) {
556             // Remove the frame from the cause trace if it is the same
557             // as in the wrapper trace
558             final String causeFrame = causeFrames.get(causeFrameIndex);
559             final String wrapperFrame = wrapperFrames.get(wrapperFrameIndex);
560             if (causeFrame.equals(wrapperFrame)) {
561                 causeFrames.remove(causeFrameIndex);
562             }
563             causeFrameIndex--;
564             wrapperFrameIndex--;
565         }
566     }
567 
568     //-----------------------------------------------------------------------
569     /**
570      * <p>Gets the stack trace from a Throwable as a String.</p>
571      *
572      * <p>The result of this method vary by JDK version as this method
573      * uses {@link Throwable#printStackTrace(java.io.PrintWriter)}.
574      * On JDK1.3 and earlier, the cause exception will not be shown
575      * unless the specified throwable alters printStackTrace.</p>
576      *
577      * @param throwable  the <code>Throwable</code> to be examined
578      * @return the stack trace as generated by the exception's
579      *  <code>printStackTrace(PrintWriter)</code> method
580      */
581     public static String getStackTrace(final Throwable throwable) {
582         final StringWriter sw = new StringWriter();
583         final PrintWriter pw = new PrintWriter(sw, true);
584         throwable.printStackTrace(pw);
585         return sw.getBuffer().toString();
586     }
587 
588     /**
589      * <p>Captures the stack trace associated with the specified
590      * <code>Throwable</code> object, decomposing it into a list of
591      * stack frames.</p>
592      *
593      * <p>The result of this method vary by JDK version as this method
594      * uses {@link Throwable#printStackTrace(java.io.PrintWriter)}.
595      * On JDK1.3 and earlier, the cause exception will not be shown
596      * unless the specified throwable alters printStackTrace.</p>
597      *
598      * @param throwable  the <code>Throwable</code> to examine, may be null
599      * @return an array of strings describing each stack frame, never null
600      */
601     public static String[] getStackFrames(final Throwable throwable) {
602         if (throwable == null) {
603             return ArrayUtils.EMPTY_STRING_ARRAY;
604         }
605         return getStackFrames(getStackTrace(throwable));
606     }
607 
608     //-----------------------------------------------------------------------
609     /**
610      * <p>Returns an array where each element is a line from the argument.</p>
611      *
612      * <p>The end of line is determined by the value of {@link SystemUtils#LINE_SEPARATOR}.</p>
613      *
614      * @param stackTrace  a stack trace String
615      * @return an array where each element is a line from the argument
616      */
617     static String[] getStackFrames(final String stackTrace) {
618         final String linebreak = SystemUtils.LINE_SEPARATOR;
619         final StringTokenizer frames = new StringTokenizer(stackTrace, linebreak);
620         final List<String> list = new ArrayList<String>();
621         while (frames.hasMoreTokens()) {
622             list.add(frames.nextToken());
623         }
624         return list.toArray(new String[list.size()]);
625     }
626 
627     /**
628      * <p>Produces a <code>List</code> of stack frames - the message
629      * is not included. Only the trace of the specified exception is
630      * returned, any caused by trace is stripped.</p>
631      *
632      * <p>This works in most cases - it will only fail if the exception
633      * message contains a line that starts with:
634      * <code>&quot;&nbsp;&nbsp;&nbsp;at&quot;.</code></p>
635      * 
636      * @param t is any throwable
637      * @return List of stack frames
638      */
639     static List<String> getStackFrameList(final Throwable t) {
640         final String stackTrace = getStackTrace(t);
641         final String linebreak = SystemUtils.LINE_SEPARATOR;
642         final StringTokenizer frames = new StringTokenizer(stackTrace, linebreak);
643         final List<String> list = new ArrayList<String>();
644         boolean traceStarted = false;
645         while (frames.hasMoreTokens()) {
646             final String token = frames.nextToken();
647             // Determine if the line starts with <whitespace>at
648             final int at = token.indexOf("at");
649             if (at != -1 && token.substring(0, at).trim().isEmpty()) {
650                 traceStarted = true;
651                 list.add(token);
652             } else if (traceStarted) {
653                 break;
654             }
655         }
656         return list;
657     }
658 
659     //-----------------------------------------------------------------------
660     /**
661      * Gets a short message summarising the exception.
662      * <p>
663      * The message returned is of the form
664      * {ClassNameWithoutPackage}: {ThrowableMessage}
665      *
666      * @param th  the throwable to get a message for, null returns empty string
667      * @return the message, non-null
668      * @since Commons Lang 2.2
669      */
670     public static String getMessage(final Throwable th) {
671         if (th == null) {
672             return "";
673         }
674         final String clsName = ClassUtils.getShortClassName(th, null);
675         final String msg = th.getMessage();
676         return clsName + ": " + StringUtils.defaultString(msg);
677     }
678 
679     //-----------------------------------------------------------------------
680     /**
681      * Gets a short message summarising the root cause exception.
682      * <p>
683      * The message returned is of the form
684      * {ClassNameWithoutPackage}: {ThrowableMessage}
685      *
686      * @param th  the throwable to get a message for, null returns empty string
687      * @return the message, non-null
688      * @since Commons Lang 2.2
689      */
690     public static String getRootCauseMessage(final Throwable th) {
691         Throwable root = ExceptionUtils.getRootCause(th);
692         root = root == null ? th : root;
693         return getMessage(root);
694     }
695 
696 }