473,547 Members | 2,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17277
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 StackTraceEleme nts, on which you can use the toString() method.

Jul 17 '05 #2
na******@inform atica.com (Naresh Agarwal) wrote in message news:<2b******* *************** ****@posting.go ogle.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(tra ceWriter, false);
excep.printStac kTrace(printWri ter);
printWriter.clo se();
String faultMessage = traceWriter.get Buffer().toStri ng();

// 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 StackTraceEleme nt. 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.printStackTra ce(new PrintWriter(sWr iter));
System.out.prin tln(sWriter.get Buffer().toStri ng());

-Nathan

na******@inform atica.com (Naresh Agarwal) wrote in message news:<2b******* *************** ****@posting.go ogle.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******@inform atica.com (Naresh Agarwal) wrote in message news:<2b******* *************** ****@posting.go ogle.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.printStackTra ce(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
5429
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 (Exception ex) { dumpEx(ex);
1
2888
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 only when exceptions are thrown? kindly reply. ..
6
1628
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 project files to a copy directory on my local wwwroot and then FTP all those files and directories to my ISP account to be able to see the change on my...
4
9182
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: ===================================== Private Sub btnCopyToClipboard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopyToClipboard.Click ...
2
6477
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 signature for method), is this possible without exception-wrapping the guts of each method? I can see how to get parameter values out of...
0
1716
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 Photo. Any suggestions on how to solve this? i can't do much at the moment. Server Error in '/PhotoGallery' Application....
1
8915
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 Photo. Any suggestions on how to solve this? i can't do much at the moment. Server Error in '/PhotoGallery' Application....
5
1941
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. -------------------------------------------------------------------------------- The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator....
8
16507
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 stack trace that caused this exception
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7463
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6032
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5362
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5081
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3493
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3473
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1923
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.