473,233 Members | 2,836 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,233 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 17264
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.