bb.util
Class ThrowableUtil

java.lang.Object
  extended by bb.util.ThrowableUtil

public final class ThrowableUtil
extends Object

Provides static utility methods for Throwables.

This class is multithread safe: it is immutable (both its immediate state, as well as the deep state of its fields).

Author:
Brent Boyer

Nested Class Summary
static class ThrowableUtil.UnitTest
          See the Overview page of the project's javadocs for a general description of this unit test class.
 
Field Summary
private static int typicalTypeAndMessageSize
           
 
Constructor Summary
private ThrowableUtil()
          This private constructor suppresses the default (public) constructor, ensuring non-instantiability.
 
Method Summary
static String getStackTraceString(StackTraceElement[] stackTrace)
          Returns a String that represent's stackTrace's elements.
static String getStackTraceString(Throwable t)
          Returns a String that represents t's stack trace.
static String getTypeAndMessage(Throwable t)
          Returns a String that concatenates t's type (i.e. classname) and message into a single String.
static RuntimeException toRuntimeException(Throwable t)
          Always returns a RuntimeException using this sequential logic: if t == null, returns a new RuntimeException which has a null cause else if t is an instanceof Error, returns a new RuntimeException which has t as its cause else if t is an instanceof RuntimeException, returns t itself else if t is an instanceof Exception, then it must be a checked Exception, so it returns a new RuntimeException which has t as its cause else t is an actual Throwable instance (or some unknown subclass), so it returns a new RuntimeException which has t as its cause This method is usually called to convert checked Exceptions into unchecked ones.
static String toString(Throwable t)
          Returns a String that represents t in its entirety.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

typicalTypeAndMessageSize

private static final int typicalTypeAndMessageSize
See Also:
Constant Field Values
Constructor Detail

ThrowableUtil

private ThrowableUtil()
This private constructor suppresses the default (public) constructor, ensuring non-instantiability.

Method Detail

toRuntimeException

public static RuntimeException toRuntimeException(Throwable t)
Always returns a RuntimeException using this sequential logic:
  1. if t == null, returns a new RuntimeException which has a null cause
  2. else if t is an instanceof Error, returns a new RuntimeException which has t as its cause
  3. else if t is an instanceof RuntimeException, returns t itself
  4. else if t is an instanceof Exception, then it must be a checked Exception, so it returns a new RuntimeException which has t as its cause
  5. else t is an actual Throwable instance (or some unknown subclass), so it returns a new RuntimeException which has t as its cause

This method is usually called to convert checked Exceptions into unchecked ones.

One example where this is useful is if you want to initialize a field by calling a method: it turns out that such a method cannot throw a checked Exception, so it must have a try-catch block, and it may be convenient for the catch to simply pass whatever it catches to this method and rethrow the result.

Parameters:
t - the original cause; may be null

toString

public static String toString(Throwable t)
                       throws IllegalArgumentException
Returns a String that represents t in its entirety. The first line in the result is the result of calling getTypeAndMessage(t). The next lines come from calling getStackTraceString(t). Finally, if t has a cause, then that Throwable's information is appended onto the result in a recursive call to this method.

Throws:
IllegalArgumentException - if t == null

getTypeAndMessage

public static String getTypeAndMessage(Throwable t)
                                throws IllegalArgumentException
Returns a String that concatenates t's type (i.e. classname) and message into a single String.

If t is an instance of ParseException, then it also appends the error offset.

Throws:
IllegalArgumentException - if t is null

getStackTraceString

public static String getStackTraceString(Throwable t)
                                  throws IllegalArgumentException
Returns a String that represents t's stack trace.

Throws:
IllegalArgumentException - if t == null

getStackTraceString

public static String getStackTraceString(StackTraceElement[] stackTrace)
                                  throws IllegalArgumentException
Returns a String that represent's stackTrace's elements.

Throws:
IllegalArgumentException - if stackTrace == null