Throwing Exceptions

J

Jonathan Wood

In code that I have enclosed in a try block, it's fairly common for me to
respond to error conditions with something like this:

throw new Exception("Something went horribly wrong!");

But I've noticed that system execptions contain information such as the line
number where the error occurred, while exceptions created using the line
above do not.

What is the secret for exceptions thrown by me to have detailed information
such as the line number?

Thanks.

Jonathan
 
I

Ignacio Machin ( .NET/ C# MVP )

In code that I have enclosed in a try block, it's fairly common for me to
respond to error conditions with something like this:

throw new Exception("Something went horribly wrong!");

But I've noticed that system execptions contain information such as the line
number where the error occurred, while exceptions created using the line
above do not.

What is the secret for exceptions thrown by me to have detailed information
such as the line number?

Thanks.

Jonathan

Hi,

It should, only that it will be where you throw the exception.
You can do several things.
- rethrow the same exception by using throw ;
- adding the thrown exception as the InnerException to the new
exception
 
J

Jonathan Wood

Sorry if I wasn't clear. There is no InnerException or exception to rethrow.
I'm creating my own exception. But it doesn't have line number information.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

message
In code that I have enclosed in a try block, it's fairly common for me to
respond to error conditions with something like this:

throw new Exception("Something went horribly wrong!");

But I've noticed that system execptions contain information such as the
line
number where the error occurred, while exceptions created using the line
above do not.

What is the secret for exceptions thrown by me to have detailed
information
such as the line number?

Thanks.

Jonathan

Hi,

It should, only that it will be where you throw the exception.
You can do several things.
- rethrow the same exception by using throw ;
- adding the thrown exception as the InnerException to the new
exception
 
J

Jonathan Wood

It doesn't include line number information for exceptions that I create.

It does have stack trace information so there's no reason to mess with that.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Peter Duniho said:
[...]
But I've noticed that system execptions contain information such as the
line number where the error occurred, while exceptions created using the
line above do not.

What is the secret for exceptions thrown by me to have detailed
information such as the line number?

The Exception instance has a StackTrace member that includes this
information.

I suppose you could also get the curent StackTrace yourself and format
that into your error message, but I'm not sure I see the point, given that
the Exception already has this information.

Pete
 
J

Jonathan Wood

Peter,
Really? When you call StackFrame.GetFileLineNumber() on the frame of the
StackTrace you're interested (the lowest frame, for example, which would
be where the exception was thrown), you get something other than the
correct line number?

I haven't tried that. Don't even know what it means. Exception doesn't have
a StackFrame member, nor does Exception.StackTrace.

What I'm doing is displaying the exception using plain-ol' ex.ToString(). In
exceptions generated by the frameworks, this produces a description of the
error, followed by stack trace information, followed by the line that
generated the error. In exceptions that I create, it produces a description
of the error, followed by stack trace information, but no line number. I'd
like my own exceptions to produce line numbers in this scenario.
The stack trace information should include line numbers for every frame in
the stack for which debug symbols exist (which presumably would include
all of your own code).

Here's what ex.ToString() produces for an exception that I created and
threw:

System.Exception: Invalid menu item information received (2975)
at ASP.fooddelete_aspx.Page_Load(Object sender, EventArgs e)
 
J

Jonathan Wood

Again, I do not want to add the line number to the Message property. I want
the line number to appear in the string returned by the ToString() method,
in just the same way it does when called on exceptions that were generated
by the framework.

I will see if I can see what you are referring to with regards to the
GetFrame() method, but it doesn't address the primary thing I'm trying to
understand/accomplish.

Jonathan
 
J

Jonathan Wood

Okay, so you're saying the even though Exception.ToString() does include
stack trace information, it does not include the line number. But that there
are system exceptions that override ToString() such that it does include the
line number.

If that's the case, I can deal with that. In fact, this is on a Web site
where I'm currently having exception data emailed to me. So I could simply
have my common handler form the proper string if regular Exceptions include
the line number.

I guess I figured that Exception.ToString() was including the line number if
it was available. So, if not, then you've helped me understand this better.

Thanks.
 
J

Jonathan Wood

Peter,
A StackTrace describes the entire call stack at the time of the
exception. A StackFrame is a single frame withing the call stack, and you
can retrieve the StackFrame instances from the StackTrace using the
GetFrame() or GetFrames() method.

An example of this would be really, REALLY helpful for me.

According to Intellisense, Exception.StackTrace is a string. Therefore, it
has no GetFrame() or GetFrames() method. I've looked high and low for these
methods. Any chance you can be a little more specific about where they are?

Jonathan
 
J

Jonathan Wood

Environment.StackTrace is also a string.

At any rate, none of this is coming together. I did some additional
searching and it looks like it might be an issue of whether or not debugging
is enabled and/or the PDB file is available. Although I don't exactly
recall, I may have been seeing those line numbers during testing on my
desktop machine, but now fail to get them on the Web server.

As near as I can tell, ex.ToString() will include stack trace information
and also line number information if it is available. It appears the
configuration on my Web server does not provide line number information.

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top