Class ExceptionOrigin

java.lang.Object
org.klojang.util.exception.ExceptionOrigin

public final class ExceptionOrigin extends Object
Provides detailed information about the origin of an exception. Useful for tracing back an exception to a statement within some code base (e.g. your own). Example:

 try {
   // stuff ...
 } catch (Exception e) {
   // Log exception message plus class and line number within the
   // com.mycompany code base where things flew off the rails
   logger.error(new ExceptionOrigin(e, "com.mycompany").getDetailedMessage());
 }
 
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new ExceptionOrigin for the provided exception, searching its stack trace for an element matching the search string.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the first stack trace element matching the search string.
    Returns the class in which the exception occurred or null if the exception came without a stack trace.
    Provides a detailed exception message that includes the class, method and line number of the first statement in the stack trace that matches the search string.
    Returns the exception wrapped by this ExceptionOrigin.
    int
    Returns the line at which the exception occurred or -1 if the exception came without a stack trace.
    Returns the method in which the exception occurred or null if the exception came without a stack trace.
    Returns the module in which the exception occurred or null if the exception came without a stack trace.
    boolean
    Returns true if the stack trace element found using the search term represents the absolute origin of the exception.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ExceptionOrigin

      public ExceptionOrigin(Throwable exc, String search)
      Creates a new ExceptionOrigin for the provided exception, searching its stack trace for an element matching the search string. Matching happens through a simple String.contains on the fully-qualified class name.
      Parameters:
      exc - the exception to analyze
      search - any part of the package name or class name that you want the exception to be traced back to.
  • Method Details

    • getDetailedMessage

      public String getDetailedMessage()
      Provides a detailed exception message that includes the class, method and line number of the first statement in the stack trace that matches the search string. Note that this may not be the absolute origin of the exception - the statement from which the exception was thrown. If the search term does happen to hit upon the absolute origin of the exception, the detailed exception message will include a notification to that effect. Otherwise the class, method and line number of the statement that did throw the exception is also included in the detailed exception message.
      Returns:
      A detailed exception message
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      getDetailedMessage()
    • getException

      public Throwable getException()
      Returns the exception wrapped by this ExceptionOrigin.
      Returns:
      the exception wrapped by this ExceptionOrigin
    • geStackTraceElement

      public StackTraceElement geStackTraceElement()
      Returns the first stack trace element matching the search string.
      Returns:
      the first stack trace element matching the search string
    • getModule

      public String getModule()
      Returns the module in which the exception occurred or null if the exception came without a stack trace.
      Returns:
      the module in which the exception occurred or null if the exception came without a stack trace
    • getClassName

      public String getClassName()
      Returns the class in which the exception occurred or null if the exception came without a stack trace.
      Returns:
      the class in which the exception occurred or null if the exception came without a stack trace
    • getMethod

      public String getMethod()
      Returns the method in which the exception occurred or null if the exception came without a stack trace.
      Returns:
      the method in which the exception occurred or null if the exception came without a stack trace
    • getLine

      public int getLine()
      Returns the line at which the exception occurred or -1 if the exception came without a stack trace.
      Returns:
      the line at which the exception occurred or -1 if the exception came without a stack trace
    • isAbsoluteOrigin

      public boolean isAbsoluteOrigin()
      Returns true if the stack trace element found using the search term represents the absolute origin of the exception. That is, whether it encapsulates the point at which the exception was thrown.
      Returns:
      true if the stack trace element found using the search term represents the absolute origin of the exception