tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mel Martinez <>
Subject Re: help: no stack trace in standard out
Date Sun, 25 Feb 2001 16:49:32 GMT
--- Jon Crater <> wrote:
> but i can't get a stack trace printed to the
> standard out to save my
> life.
> any ideas?

Whether or not a stack trace gets printed is not a
function  of the logging level.  A stack trace will be
printed under the following conditions:

If an uncaught exception makes it through to the VM
runtime, a trace will printed by the runtime to
standard err (System.err).

If a catch() block in your code (or someone else's
code higher up the call chain) catches an exception
and proceeds to  explicitely print the stack trace.
Note - if you are throwing an exception that you
expect to show a trace at the runtime level, it is
possible that "someone else's code" is catching it and
ignoring it (a very bad practice but all too common).

Finally, you can always just create a Throwable object
and print the stack trace yourself.

To display the stack trace of a Throwable object to
System.err, you can just use the
Throwable.printStackTrace() method.  Thus, in your
code you might put:

  // do something that throws a FooException
}catch(FooException e){

Or, for debug purposes to find out how you got to your
code without relying on some Exception being thrown,
just do the following:

   // stuff...
   (new Throwable()).printStackTrace();
   //... continue doing other stuff...

The only problem with the above technique is that
there is no control over formatting or where the
message goes.  It always goes straight to stderr.  To
change that, you can capture the stack trace in a
String using a simple method like so:

  utility function for retrieving the stacktrace of
  a Throwable object as a String for easy
  Useful for debugging.  Returns "" if <i>t</i>==null.
  @param <b>t</b> a Throwable object.
  @return a String representation of the stack trace
          for <i>t</i>.
public static String getStackTrace(Throwable t){
    return "";
  StringWriter sw = new StringWriter();
  t.printStackTrace(new PrintWriter(sw));
  return sw.toString();		

Put the above method into a utility class (i.e.
'MyUtils' and then you can always grab the trace for a
throwable anywhere in your code with:

  String trace=MyUtils.getStackTrace(new Throwable());

That way, you can display the trace in an html page,
or search it or make it part of the message nested
inside another exception, or whatever.

Using stack traces like this is a very useful tool for
developing and debugging code, or for simply trying to
understand other folk's code.  Visual Debuggers are
useful too, but less reliable and not ubiquitous.

One tip:  You refer to wanting to print the trace to
'standard out'.  Just a bit of developer's philosophy,

 Standard out - for program _output_ (i.e., the actual
  product of the application).  Example: 'more'
  displays text from files to stdout.
 Standard err - for program _debug_ and _error_
  messages.  Example, if 'more' can't find the file
  indicated, it prints an error message to stderr.

The distinction is important because conceptually,
these are two different io streams.  By default,
System.err prints to the same OutputStream as
System.out, but that is not necessarily always so. 
You can easily redirect one or the other using
System.setErr() or System.setOut().  By always sending
your debug and error messages to System.err, you
should not have to worry about forgetting to remove
all your debug statements and accidentally corrupting
your program's output.

Just my $.02.  Take it for what it's worth.  :-)

I hope this is helpful.


Do You Yahoo!?
Get email at your own domain with Yahoo! Mail.

View raw message