472,121 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

Can we copy stack trace of Excepion in a string?

Hi

Is there a way to copy the stack trace in a string, which is obtained
via printStackTrace function of Exception object.

Essentially I want to log the stack trace in case of any exception in
a log file.

thanks,
Naresh
Jul 17 '05 #1
4 17170
Naresh Agarwal wrote:
Is there a way to copy the stack trace in a string, which is obtained
via printStackTrace function of Exception object.


Yes, the Exception object will have a method getStackTrace which gives an
array of StackTraceElements, on which you can use the toString() method.

Jul 17 '05 #2
na******@informatica.com (Naresh Agarwal) wrote in message news:<2b**************************@posting.google. com>...
Hi

Is there a way to copy the stack trace in a string, which is obtained
via printStackTrace function of Exception object.

Essentially I want to log the stack trace in case of any exception in
a log file.

thanks,
Naresh


catch (Exception excep)
{
StringWriter traceWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(traceWriter, false);
excep.printStackTrace(printWriter);
printWriter.close();
String faultMessage = traceWriter.getBuffer().toString();

// do something with faultMessage
}

There are probably more elegant ways to do this, but I think it should work.
Stian
Jul 17 '05 #3
There are two ways you can do this.

The first is to use the getStackTrace() method, which returns an array
of StackTraceElement. You can loop through that array and and push
the contents into your log. The advantage to this is that you can
just log the parts of the stack trace you are interested in.

The second way is to create a StringWriter class to capture the output
from the Exception using the printStackTrace(PrintWriter) method.
Here is some sample code:

StringWriter sWriter = new StringWriter();
e.printStackTrace(new PrintWriter(sWriter));
System.out.println(sWriter.getBuffer().toString()) ;

-Nathan

na******@informatica.com (Naresh Agarwal) wrote in message news:<2b**************************@posting.google. com>...
Hi

Is there a way to copy the stack trace in a string, which is obtained
via printStackTrace function of Exception object.

Essentially I want to log the stack trace in case of any exception in
a log file.

thanks,
Naresh

Jul 17 '05 #4
na******@informatica.com (Naresh Agarwal) wrote in message news:<2b**************************@posting.google. com>...
Hi

Is there a way to copy the stack trace in a string, which is obtained
via printStackTrace function of Exception object.

Essentially I want to log the stack trace in case of any exception in
a log file.

thanks,
Naresh


Naresh:

Yes, this is possible. There is a version of the printStackTrace
method which takes a PrintWriter as an argument. See the following
example:

try {
//something
}
catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
String myException = sw.toString();
sw.close();
pw.close();
}
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike Schilling | last post: by
1 post views Thread by | last post: by
6 posts views Thread by Wayne Wengert | last post: by
4 posts views Thread by Wayne Wengert | last post: by
2 posts views Thread by news.microsoft.com | last post: by
reply views Thread by Patrick F | last post: by
1 post views Thread by Patrick F | last post: by
5 posts views Thread by Mr. SweatyFinger | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.