1 23 24 29 30 package com.sun.appserv.management.util.misc; 31 32 import java.util.ArrayList ; 33 34 35 38 public final class ExceptionUtil 39 { 40 private 41 ExceptionUtil() 42 { 43 } 45 46 public static String 47 toString( final Throwable t ) 48 { 49 final String SEP = System.getProperty( "line.separator" ); 50 51 final Throwable rootCause = getRootCause( t ); 52 53 return rootCause.getClass().getName() + ": " + 54 StringUtil.quote( rootCause.getMessage() ) + SEP + 55 getStackTrace( rootCause ); 56 } 57 58 65 public static Throwable [] 66 getCauses( final Throwable start ) 67 { 68 final ArrayList <Throwable > list = new ArrayList <Throwable >(); 69 70 boolean haveNonException = false; 71 72 Throwable t = start; 73 while ( t != null ) 74 { 75 list.add( t ); 76 77 if ( ! ( t instanceof Exception ) ) 78 { 79 haveNonException = true; 80 } 81 82 final Throwable temp = t.getCause(); 83 if ( temp == null ) 84 break; 85 t = temp; 86 } 87 88 final Throwable [] results = haveNonException ? 89 new Throwable [ list.size() ] : new Exception [ list.size() ]; 90 91 list.toArray( results ); 92 93 return( results ); 94 } 95 96 97 103 public static Throwable 104 getRootCause( final Throwable e ) 105 { 106 final Throwable [] causes = getCauses( e ); 107 108 return( causes[ causes.length - 1 ] ); 109 } 110 111 117 public static String 118 getStackTrace( Throwable t ) 119 { 120 final StringBuffer buf = new StringBuffer (); 121 final StackTraceElement [] elems = t.getStackTrace(); 122 123 for( int i = 0; i < elems.length; ++i ) 124 { 125 buf.append( elems[ i ] ); 126 buf.append( "\n" ); 127 } 128 129 130 return( buf.toString() ); 131 } 132 } 133 134 | Popular Tags |