FileDocCategorySizeDatePackage
ExceptionUtil.javaAPI DocGlassfish v2 API4387Fri May 04 22:31:06 BST 2007com.sun.appserv.management.util.misc

ExceptionUtil

public final class ExceptionUtil extends Object
Useful utilities for Exceptions

Fields Summary
Constructors Summary
private ExceptionUtil()

		// disallow instantiation
	
Methods Summary
public static java.lang.Throwable[]getCauses(java.lang.Throwable start)
Get the chain of exceptions via getCause(). The first element is the Exception passed.

param
start the Exception to traverse
return
a Throwable[] or an Exception[] as appropriate

		final ArrayList<Throwable>	list	= new ArrayList<Throwable>();
		
		boolean	haveNonException	= false;
		
		Throwable t	= start;
		while ( t != null )
		{
			list.add( t );
			
			if ( ! ( t instanceof Exception ) )
			{
				haveNonException	= true;
			}
			
			final Throwable temp	= t.getCause();
			if ( temp == null )
				break;
			t	= temp;
		}
		
		final Throwable[]	results	= haveNonException ?
			new Throwable[ list.size() ] : new Exception[ list.size() ];
		
		list.toArray( results );
		
		return( results );
	
public static java.lang.ThrowablegetRootCause(java.lang.Throwable e)
Get the original troublemaker.

param
e the Exception to dig into
return
the original Throwable that started the problem

		final Throwable[]	causes	= getCauses( e );
		
		return( causes[ causes.length - 1 ] );
	
public static java.lang.StringgetStackTrace()

        return toString(new Exception("STACK TRACE"));
    
public static java.lang.StringgetStackTrace(java.lang.Throwable t)
Get the stack trace as a String.

param
t the Throwabe whose stack trace should be gotten
return
a String containing the stack trace

		final StringBuffer	buf	= new StringBuffer();
		final StackTraceElement[]	elems	= t.getStackTrace();
		
		for( int i = 0; i < elems.length; ++i )
		{
			buf.append( elems[ i ] );
			buf.append( "\n" );
		}
		
		
		return( buf.toString() );
	
public static java.lang.StringtoString(java.lang.Throwable t)

	    final String SEP    = System.getProperty( "line.separator" );
	    
	    final Throwable rootCause   = getRootCause( t );
	    
	    return rootCause.getClass().getName() + ": " +
	        StringUtil.quote( rootCause.getMessage() ) + SEP +
	        getStackTrace( rootCause );