Printing a stack trace

M

Mark Space

Hi all,

Just curious: is there a better way to print a stack trace? I want to
be able to log them if I have to, so I'm converting the StacTrace to a
string. I wonder if there's a better/easier way to do this however.

Code fragments follow:

// Static method inside class IoUtil:
public static String StackTraceToString( Exception e )
{
ByteArrayOutputStream b = new ByteArrayOutputStream();
PrintStream p = new PrintStream( b );
e.printStackTrace( p );
p.flush();
return b.toString();
}

// Example use:
catch( IOException e )
{
System.err.println( "IO error while reading!" + e );
String st = IoUtil.StackTraceToString( e );
System.err.println( st );
this.log.fine( "IO error while reading!" );
this.log.fine( st );

}

The example use has an error with respect to non-static variables, but
ignore that and just focus on the stack trace part. Any comments much
appreciated.
 
L

lordy

Just curious: is there a better way to print a stack trace?
e.printStackTrace(System.err);



I want to
be able to log them if I have to, so I'm converting the StacTrace to a
string. I wonder if there's a better/easier way to do this however.

If this is your own logging class
Create a logging method that takes a Throwable t and calls..

t.printStackTrace(log.outputStream);

Or see throwable class for more..

Better still use the appropiate method in a pre-written logging class.

Eg. for java.util.logging.Logger class ..

logger.log(Level.FINE,"IO Error",e);

See logger class for more ...

Although I find that most of the time I log exceptions at a WARING or SEVERE
level.

Lordy
 
M

Mark Space

lordy said:
If this is your own logging class
Create a logging method that takes a Throwable t and calls..

t.printStackTrace(log.outputStream);

Thanks, this is an interesting idea. Maybe a tad less generic that just
making a String, but I might give this a try to see if I like it better.
Or see throwable class for more..

Better still use the appropiate method in a pre-written logging class.

Eg. for java.util.logging.Logger class ..

logger.log(Level.FINE,"IO Error",e);

This uses the toString() method in the Throwable class, yes? Does
toString() print the stack trace as well? I thought the Java Doc
implied that toString() did not. That's why they have a seperate method
for print the stack trace.
See logger class for more ...

Although I find that most of the time I log exceptions at a WARING or SEVERE
level.

The idea is that a stack trace is only printed if the log level has been
increased. If the logging is at the default level, just printing the
error message is enough. Though maybe on a pragmatic level you are correct.
 
L

lordy

Thanks, this is an interesting idea. Maybe a tad less generic that just
making a String, but I might give this a try to see if I like it better.


This uses the toString() method in the Throwable class, yes?

Nope :) It is passed as a throwable to the logRecord and in turn to the
Handler.

http://java.sun.com/j2se/1.4.2/docs...Level, java.lang.String, java.lang.Throwable)

"Note that the thrown argument is stored in the LogRecord thrown
property"

See also
http://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/LogRecord.html

Lordy
 
I

Ingo R. Homann

Hi,

Mark said:
Just curious: is there a better way to print a stack trace? I want to
be able to log them if I have to, so I'm converting the StacTrace to a
string. I wonder if there's a better/easier way to do this however.

In addition to the others: Since Java5, there is the method
getStackTrace() which may help you.

Ciao,
Ingo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top