Presents your JAVA E-NEWSLETTER for November 13, 2003 <-------------------------------------------> PINPOINTING AN EXCEPTION WITH GETSTACKTRACE() Some Java e-newsletter subscribers have expressed their frustration with nested exceptions, saying that they would rather know how to get less information from exceptions instead of more. While it's never a good idea to discard information that can be used for debugging, sometimes less can be more, and Java 1.4 gives you a tool that allows you to pinpoint the cause of an error. This tool is the getStackTrace() utility, which is in the Throwable class. getStackTrace() returns an array of StackTraceElement objects that were created when the Throwable object was created. You can query these StackTraceElements to find out exactly where the error occurred. The returned array contains one element for each method that was invoked during the call, plus one more element for the code that is making the call. The first element of the array indicates the method that threw the original exception. To get the information you want, simply call the appropriate methods. Here is an example that illustrates the use of getStackTrace(): public class StackTraceTip { public static void main(String args[]) { StackTraceTip s = new StackTraceTip(); try { s.foo(Integer.parseInt(args[0])); } catch (Exception e) { Throwable t = getRootCause(e); // print out root cause and handled exception for // comparison System.out.println(e); System.out.println(t); StackTraceElement []trace = t.getStackTrace(); StackTraceElement ste = trace[0]; System.err.println("error occurred in method: " + ste.getMethodName()); System.err.println(" file: " + ste.getFileName()); System.err.println(" line number: " + ste.getLineNumber()); } } public void foo(int y) { if ( y > 1 ) { throw new IllegalArgumentException("invalid value: " + y); } try { bar(y); } catch (IllegalArgumentException e) { throw new RuntimeException("bar threw error", e); } } public void bar(int x) { if ( x < 1 ) { throw new IllegalArgumentException("invalid value: " + x); } } public static Throwable getRootCause(Exception e) { Throwable root = e; while (root.getCause() != null) { root = root.getCause(); } return root; } } The amount of information available in the stack trace depends on how the code was compiled--with or without debugging information--and the virtual machine that the code is running in. So, if you use this method to get information, be prepared to handle zero-length arrays and StackTraceElements with no file names or line numbers. David Petersheim is the Director of Application Development with Genscape, Inc. He designs and develops server-side applications to acquire and process real-time energy data. ----------------------------------------