More Java posts
May 16, 2002
Simple Java "Trick"

You've all probably seen this one before but I find it useful during forensic investigation of programs. The general situation is

  • You're working on a large mass of code you understand poorely.
  • You can see where the problem is being manifested, generally because you can see where an Exception is being thrown from.
  • The underlying cause is that some other method is earlier called with an incorrect value, and you can find that method.
  • You don't know the program flow well enough to know where this earlier method is called from, and that's where you want to look for the fundamental problem.

Fortunately you can use an Exception to generate a stack trace without having to throw the Exception.

if ((prefix != null) && prefix.equals("xmlns")) {
    Exception e = new Exception();
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    e.printStackTrace(pw);
    pw.flush();
    log.debug("getNamespaceURI(...) called from " +
              sw.toString());
}
Posted by Alex at May 16, 2002 08:05 PM
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?