473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 17269
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Schilling | last post by:
Instances of SystemOutOfMemoryException do not contain a stack trace. Easy test to verify this: class OOM { public static void Main() { try { Object arr = new Object; } catch...
1
by: | last post by:
Hi all i posted this question yesterday. no answers yet. please do reply if you have any ideas. thanks a lot. Subject: stack frame stack trace can the information from the stack be obtained...
6
by: Wayne Wengert | last post by:
I am using VSNET 2003 to build an ASP.NET/VB set of pages. There are currently about a dozen aspx pages. When I make even a minor change to one page I currently rebuild the solution, copy the...
4
by: Wayne Wengert | last post by:
I have an aspx page on which I am trying to copy the contents of a textbox to the client clipboard when the users clicks a button. The button code is as follows:...
2
by: news.microsoft.com | last post by:
Hi all. If I wanted to write something so that, when an exception was thrown, and the stack unwound, the stack trace was captured with the values of the parameters (instead of just the parameter...
0
by: Patrick F | last post by:
I keep getting this error message and i don't know whats the problem is and how to solve it. I have a webproject with 2 components (dll) added to it. One is called PhotoGallery and one is called...
1
by: Patrick F | last post by:
I keep getting this error message and i don't know whats the problem is and how to solve it. I have a webproject with 2 components (dll) added to it. One is called PhotoGallery and one is called...
5
by: Mr. SweatyFinger | last post by:
WHY CAN'T THE CLOWN -HOLES WHO WROTE ASP.NET PROVIDE AN ERROR LINE NUMBER??? HONEST TO SH@THOLE PETE Server Error in '/New Folder (7)' Application....
8
by: indiarocks | last post by:
Hi, Can a stack trace be generated for the exception that the program catches using except or except IOError etc eg: try: raise Exception except Exception,e: print the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.