Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Printing a stack trace

Reply
Thread Tools

Printing a stack trace

 
 
Mark Space
Guest
Posts: n/a
 
      07-10-2006
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.
 
Reply With Quote
 
 
 
 
lordy
Guest
Posts: n/a
 
      07-11-2006
On 2006-07-10, Mark Space <> wrote:
>
> 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
 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      07-11-2006
lordy wrote:
> 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.

>
> Lordy

 
Reply With Quote
 
lordy
Guest
Posts: n/a
 
      07-11-2006
On 2006-07-11, Mark Space <> wrote:
> lordy wrote:
>> 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?


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/...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/...LogRecord.html

Lordy

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-11-2006
lordy wrote:
> 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/...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/...LogRecord.html
>
> Lordy
>


Ah! Thanks for the pointers. That'll give me something to chew on for
this evening.
 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      07-11-2006
Hi,

Mark Space wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
No trace messages using Diagnostics.Trace McGeeky ASP .Net 0 02-01-2006 02:49 PM
How to redirect output from Diagnostics.Trace to Page.Trace? Matthias S. ASP .Net 1 11-30-2005 09:01 AM
printing a stack trace ... Gernot Hoebenreich C++ 1 04-01-2005 08:24 PM
Trace: Can anyone suggest a good tool to catch trace messages? Rukmal Fernando ASP .Net 4 10-27-2003 09:03 PM



Advertisments