Class ExceptionUtil

java.lang.Object
com.randomnoun.common.ExceptionUtil

public class ExceptionUtil extends Object
Exception utilities class.

This class contains static utility methods for handling and manipulating exceptions; the only one you're likely to call being getStackTraceWithRevisions(Throwable, ClassLoader, int, String), which extracts CVS revision information from classes to produce an annotated (and highlighted) stack trace.

When passing in ClassLoaders to these methods, you may want to try one of

  • this.getClass().getClassLoader()
  • Thread.currentThread().getContextClassLoader()
Author:
knoxg
See Also:
  • Field Details

  • Method Details

    • getStackTrace

      public static String getStackTrace(Throwable e)
      Converts an exception's stack trace to a string. If the exception passed to this function is null, returns the empty string.
      Parameters:
      e - exception
      Returns:
      string representation of the exception's stack trace
    • getStackTraceWithRevisions

      public static String getStackTraceWithRevisions(Throwable e, ClassLoader loader, int highlight, String highlightPrefix)
      Converts an exception's stack trace to a string. Each stack trace element is annotated to include the CVS revision Id, if it contains a public static String element containing this information.

      Stack trace elements whose classes begin with the specified highlightPrefix are also marked, to allow easier debugging. Highlights used can be text (which will insert the string "=>" before relevent stack trace elements), or HTML (which will render the stack trace element between <b> and </b> tags.

      If HTML highlighting is enabled, then the exception message is also HTML-escaped.

      Parameters:
      e - exception
      loader - ClassLoader used to read stack trace element revision information
      highlight - One of the HIGHLIGHT_* constants in this class
      highlightPrefix - A prefix used to determine which stack trace elements are rendered as being 'important'. (e.g. "com.randomnoun.common."). Multiple prefixes can be specified, if separated by commas.
      Returns:
      string representation of the exception's stack trace
    • getStackDepth

      public static int getStackDepth()
      Return the number of elements in the current thread's stack trace (i.e. the height of the call stack).
      Returns:
      the height of the call stack
    • getClassRevision2

      An alternate implementation of getClassRevision(ClassLoader, String), which searches the raw bytecode of the class, rather than using Java reflection. May be a bit more robust.
      Parameters:
      loader - Classloader to use
      className - Class to load
      Returns:
      The CVS revision string, in the form "ver 1.234".
      Throws:
      ClassNotFoundException
      SecurityException
      NoSuchFieldException
      IllegalArgumentException
      IllegalAccessException
    • getStackTraceSummary

      public static List<String> getStackTraceSummary(Throwable throwable)
      Returns a list of all the messages contained within a exception (following the causal chain of the supplied exception). This may be more useful to and end-user, since it should not contain any references to Java class names.
      Parameters:
      throwable - an exception
      Returns:
      a List of Strings
    • escapeHtml

      public static String escapeHtml(String string)
      Returns the HTML-escaped form of a string. Any &, <, >, and " characters are converted to &amp;, &lt;, &gt;, and &quot; respectively.
      Parameters:
      string - the string to convert
      Returns:
      the HTML-escaped form of the string