Stack Traces


Lesson 4 taught you how to read the exception stack to help decipher the source of an exception.

You can send the stack trace stored within an exception to a print stream or print writer using the printStackTrace method defined in Throwable. One implementation of printStackTrace takes no parameters, printing the stack trace on the system console (System.out) by default. A crude implementation of the log method might do just that.

 private void log(Exception e) {    e.printStackTrace(); } 

(Try it.) For a production system, you will want a more robust logging solution. Sun introduced a logging API in J2SE 1.4; I discuss logging in the second half of this lesson.

The stack trace representation printed by printStackTrace contains information that might be useful for more advanced programming needs. You could parse the stack trace yourself in order to obtain the information, which many developers have done. Or, as of J2SE 1.4, you can send the message getStackTrace to the exception object in order to access the information in already parsed form.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net