473,325 Members | 2,860 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,325 software developers and data experts.

accessing StackTrace inside an exception ?

Hi,

I am throwing an exception derived from ApplicationException as follows
catch (Exception ex) {
throw new MyException("message", ex);
}

My exception's constructor is:
public ExceptionBase(string aMessage, Exception aInnerException):
base(aMessage, aInnerException)

In the constructor I am finding that "this.StackTrace" is blank, while
"aInnerException.StackTrace" is populated with class name, method and
failing line number as expected. "this.Message" works as expected.
"this.GetType().Name" also works ok.

Is my exception's StackTrace property unpopulated because I am still
inside the Exception's constructor. If so, how do I write MyException
code that will fire after the constructor has completely built the
Exception ?

thanks

Barry Mossman
Nov 16 '05 #1
4 4366
you can use Environment.StackTrace to find out the stack trace.

Av
http://dotnetjunkies.com/WebLog/avnrao
http://www28.brinkster.com/avdotnet

"Barry Mossman" <BM************@gmail.com> wrote in message
news:O9*************@TK2MSFTNGP11.phx.gbl...
Hi,

I am throwing an exception derived from ApplicationException as follows
catch (Exception ex) {
throw new MyException("message", ex);
}

My exception's constructor is:
public ExceptionBase(string aMessage, Exception aInnerException):
base(aMessage, aInnerException)

In the constructor I am finding that "this.StackTrace" is blank, while
"aInnerException.StackTrace" is populated with class name, method and
failing line number as expected. "this.Message" works as expected.
"this.GetType().Name" also works ok.

Is my exception's StackTrace property unpopulated because I am still
inside the Exception's constructor. If so, how do I write MyException
code that will fire after the constructor has completely built the
Exception ?

thanks

Barry Mossman

Nov 16 '05 #2

"avnrao" <av*@newsgroups.com> wrote in message
news:O5**************@TK2MSFTNGP12.phx.gbl...
you can use Environment.StackTrace to find out the stack trace.


Hi, thanks for the response.

I had tried Environment.StackTrace, but it gives me a great long string
showing all stack frames with the point that actually produced the error
n levels deep, depending upon how many overloaded constructors by
Exception class has, and the various calls within the System.Envionment
namespace to produce the answer.

The StackTrace property for the inner exception just gives me information
on just the stack frame that caused the inner exception.

At the moment I am creating my own StackTrace, and then discarding enough
entries to account for the overloaded constructors involved in
constructing my exception. I then take just the next frame to identify
the method name & line number that caused the problem.

I was hoping for something more elegant, ie. how to use the Exception's
StackFrame property AFTER it was populated, but before I lose control due
to the thrown exception.

Barry Mossman
Nov 16 '05 #3
The stack trace that is in the exception object's StackTrace property starts
where the exception was thrown and ends at the current stack frame (i.e.
where it is caught). It can't fill in the stack trace of the new exception
object because not only has it not been caught (thus reaching the end of the
stack trace), it hasn't been thrown yet so it does not know where it should
start.

One reason for this is because simply creating an exception object does not
mean that it is immediately thrown - you can create on exception object and
use it anywhere, or reuse the same one - so the runtime does not fill in the
stack trace at the moment it is created.

The stack trace ends where it is caught - my understanding is that one
reason was for performance. It is faster to end the stack trace there rather
then walk the entire stack. You can do that on your own if you need the full
stack trace.


"Barry Mossman" <BM************@gmail.com> wrote in message
news:O9*************@TK2MSFTNGP11.phx.gbl...
Hi,

I am throwing an exception derived from ApplicationException as follows
catch (Exception ex) {
throw new MyException("message", ex);
}

My exception's constructor is:
public ExceptionBase(string aMessage, Exception aInnerException):
base(aMessage, aInnerException)

In the constructor I am finding that "this.StackTrace" is blank, while
"aInnerException.StackTrace" is populated with class name, method and
failing line number as expected. "this.Message" works as expected.
"this.GetType().Name" also works ok.

Is my exception's StackTrace property unpopulated because I am still
inside the Exception's constructor. If so, how do I write MyException code
that will fire after the constructor has completely built the Exception ?

thanks

Barry Mossman

Nov 16 '05 #4
"David Levine" <no****************@wi.rr.com> wrote in message
news:eh**************@TK2MSFTNGP15.phx.gbl...
The stack trace that is in the exception object's StackTrace property
starts where the exception was thrown and ends at the current stack
frame (i.e. where it is caught). It can't fill in the stack trace of
the new exception object because not only has it not been caught (thus
reaching the end of the stack trace), it hasn't been thrown yet so it
does not know where it should start.

One reason for this is because simply creating an exception object does
not mean that it is immediately thrown - you can create on exception
object and use it anywhere, or reuse the same one - so the runtime does
not fill in the stack trace at the moment it is created.

The stack trace ends where it is caught - my understanding is that one
reason was for performance. It is faster to end the stack trace there
rather then walk the entire stack. You can do that on your own if you
need the full stack trace.


Thanks David, it makes more sense to me now.

What I am seeing is an empty string while I am in the constructor, which
fits it in with what you are saying.

As I said in a different post to the one that you answered, I have a work
around where I create my own StackTrace inside the exception's
constructor, and then walk the frames until I found the one that I
thought triggered the exception. This sounds like it will be my final
solution.

Thanks for explaining the rationale behind what i am observing.

Barry Mossman

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Will Ware | last post by:
I was fooling with some Python code, and starting to miss the Exception.printStackTrace() feature in Java. Here is a stab at something roughly analogous, which puts together a stacktrace as an XML...
0
by: lajo_79 | last post by:
my service has a functions that gets triggered by a timer.. my code catches a nullref exception but i cant get the stacktrace for it... i only get information about which function that generated...
11
by: Yahoo | last post by:
I have a programming issue where I need to know the whole history of the call stack to ensure it was never within a given method (specifically my own method). I am hooking into the XmlDocument...
4
by: Khalique | last post by:
I have built a web service whose purpose is to copy files from a secure place to client machine and vice versa. The problem I am having is perhaps related to permissions and access rights. For...
3
by: Mark R. Dawson | last post by:
Hi all, I am trying to get custom attributes from a property. I can do this if I pass in the name of the property i.e. "Name" to the reflection methods, but if I pass in set_Name which is what...
0
by: Kieran Toon | last post by:
I'm using Visual Studio 2005 and have an ASP.NET web application written in Visual Basic. When the application runs on my local webserver and hits an error, my errorhandler writes out debug...
0
by: rkr31 | last post by:
Hi I have an iis6 webserver serving pages from a clustered share. classc asp and html pages work ok but it will not serve aspx pages. When i try i get the following error: Server...
3
by: Samuel | last post by:
Hi, I am trying to wrap a function that throws an exeption in such a way that the stacktrace is logged into a file and the exception is forwarded after that. For example: -------------------...
6
by: Kristijan Marin | last post by:
Hi, Can please anyone tell me why do i get only the first method in which the exception was thrown and not the line that triggered the exception ? I don't know if I set something in VS2005 wrong...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.