473,466 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bug?! throw vs. throw ex

I want to "rethrow" an exception so as not to lose the original stack
trace information. I understood that I could use throw, like:

try {
DoIt();
}
catch (Exception ex) {
DoSomeExceptionStuff(ex);
throw;
}
However, this apparently does NOT preserve the stack trace--it loses it
just like using throw ex; does! I am currently using .NET 1.1 SP1. Is
this a known bug? Why do so many people report that it works for them?
Have they actually tried it?

Why would a bug like this exist in v. 1.1 SP1?
Can anyone help shed some light on this?

Thanks in advance.

Nov 16 '05 #1
12 4300
the rethrow does not preserve the stack. it is not a bug. You need preserve
the stack and then throw a new exception with the amended stack

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------
"GoogleNewsReaderMan" <lo***********@hotmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I want to "rethrow" an exception so as not to lose the original stack
trace information. I understood that I could use throw, like:

try {
DoIt();
}
catch (Exception ex) {
DoSomeExceptionStuff(ex);
throw;
}
However, this apparently does NOT preserve the stack trace--it loses it
just like using throw ex; does! I am currently using .NET 1.1 SP1. Is
this a known bug? Why do so many people report that it works for them?
Have they actually tried it?

Why would a bug like this exist in v. 1.1 SP1?
Can anyone help shed some light on this?

Thanks in advance.

Nov 16 '05 #2
> I want to "rethrow" an exception so as not to lose the original stack
trace information.


I believe the stack trace is populated as part of the throw, not when the
exception object is created.

Create a new exception and stuff the old one inside it as the "inner
exception". The new one should have the new stack trace and the old one the
original stack trace.
Nov 16 '05 #3
Not sure if it works, but try

throw new Exception("some message", ex)

"GoogleNewsReaderMan" <lo***********@hotmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I want to "rethrow" an exception so as not to lose the original stack
trace information. I understood that I could use throw, like:

try {
DoIt();
}
catch (Exception ex) {
DoSomeExceptionStuff(ex);
throw;
}
However, this apparently does NOT preserve the stack trace--it loses it
just like using throw ex; does! I am currently using .NET 1.1 SP1. Is
this a known bug? Why do so many people report that it works for them?
Have they actually tried it?

Why would a bug like this exist in v. 1.1 SP1?
Can anyone help shed some light on this?

Thanks in advance.

Nov 16 '05 #4
Yes, it's a bug. It is supposed to be fixed in Whidbey. For now you will
need to wrap the exception in a new exception object and use the original
exception as the inner exception.

"GoogleNewsReaderMan" <lo***********@hotmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I want to "rethrow" an exception so as not to lose the original stack
trace information. I understood that I could use throw, like:

try {
DoIt();
}
catch (Exception ex) {
DoSomeExceptionStuff(ex);
throw;
}
However, this apparently does NOT preserve the stack trace--it loses it
just like using throw ex; does! I am currently using .NET 1.1 SP1. Is
this a known bug? Why do so many people report that it works for them?
Have they actually tried it?

Why would a bug like this exist in v. 1.1 SP1?
Can anyone help shed some light on this?

Thanks in advance.

Nov 16 '05 #5
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
the rethrow does not preserve the stack. it is not a bug. You need preserve
the stack and then throw a new exception with the amended stack


Not true. Try this:

using System;

class Test
{
static void Main()
{
try
{
DoSomething();
}
catch (Exception e)
{
Console.WriteLine (e);
}
}

static void DoSomething()
{
try
{
DoSomethingNaughty();
}
catch (Exception)
{
throw;
}
}

static void DoSomethingNaughty()
{
string x = null;
bool y = x.Equals("hello");
}
}

Compile it with debug turned on and run it. I get a stack trace of:
System.NullReferenceException: Object reference not set to an instance
of an object.
at Test.DoSomethingNaughty() in c:\test\Test.cs:line 32
at Test.DoSomething() in c:\test\Test.cs:line 25
at Test.Main() in c:\test\Test.cs:line 9

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Jako Menkveld wrote:
Not sure if it works, but try

throw new Exception("some message", ex)


That would give you a new exception, with this place as stack-trace and
the old exception (with the old stack-trace) as it's "Inner" exception.

I have come to almost loathe the above way of coding, since it means
that catchers cannot use the type of the exception to dispatch to a
catch-handler but has to catch Exception and to analysis on the content
of e.Inner.

This is especially interesting with Delegate, which throws a wrapped
exception if invoking the Delegate throws an exception (/me holds back a
swarm of swearwords).

The difference between "throw e;" and "throw;" is that "throw;" rethrows
the original catch'ed exception unchanged (like in C++) whereas "throw
e"; throws the expression e, which happens to refer to a catch'ed
exception object.

--
Helge
Nov 16 '05 #7
I get the same results as you. I stand corrected.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ http://tinyurl.com/27cok
----------------------------------------------------------
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
the rethrow does not preserve the stack. it is not a bug. You need
preserve
the stack and then throw a new exception with the amended stack


Not true. Try this:

using System;

class Test
{
static void Main()
{
try
{
DoSomething();
}
catch (Exception e)
{
Console.WriteLine (e);
}
}

static void DoSomething()
{
try
{
DoSomethingNaughty();
}
catch (Exception)
{
throw;
}
}

static void DoSomethingNaughty()
{
string x = null;
bool y = x.Equals("hello");
}
}

Compile it with debug turned on and run it. I get a stack trace of:
System.NullReferenceException: Object reference not set to an instance
of an object.
at Test.DoSomethingNaughty() in c:\test\Test.cs:line 32
at Test.DoSomething() in c:\test\Test.cs:line 25
at Test.Main() in c:\test\Test.cs:line 9

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8
One arguable flaw here, though, is that you lose the location within
the DoSomething() method --- the line number is that of the throw. So,
in fact, the stack trace is modified, and not that of the original
exception.
Jon wrote:
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
the rethrow does not preserve the stack. it is not a bug. You need preserve the stack and then throw a new exception with the amended stack
Not true. Try this:

using System;

class Test
{
static void Main()
{
try
{
DoSomething();
}
catch (Exception e)
{
Console.WriteLine (e);
}
}

static void DoSomething()
{
try
{
DoSomethingNaughty();
}
catch (Exception)
{
throw;
}
}

static void DoSomethingNaughty()
{
string x = null;
bool y = x.Equals("hello");
}
}

Compile it with debug turned on and run it. I get a stack trace of:
System.NullReferenceException: Object reference not set to an

instance of an object.
at Test.DoSomethingNaughty() in c:\test\Test.cs:line 32
at Test.DoSomething() in c:\test\Test.cs:line 25
at Test.Main() in c:\test\Test.cs:line 9

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #9
Gentlemen,

There seem to be a lot of experts replying to this thread so I will add
my own question. If you wanted to get the same exception at a higher
level, why would you catch it at all?

for example (modifying the previous code):

using System;

class Test
{
static void Main()
{
try
{
DoSomething();
}
//handle all exceptions without bias on moral behavior
catch (Exception e)
{
Console.WriteLine (e);
}
}

static void DoSomething()
{
try
{
DoSomethingNaughty();
}
catch (NaughtyException)
{
//handle the offending problem
}
//let all of the less ethicly challenged exceptions bubble up
the the calling Main()
}

static void DoSomethingNaughty()
{
string x = null;
bool y = x.Equals("hello");
}
}

If I wanted to do some intermediate processing in the routine and then
still post an error to the calling method, I would throw a new
exception (of whatever type) with the original exception as the inner
exception. Does stuffing the old exception into the new still mess up
the stack?

Thanks,
Dan

Nov 16 '05 #10
There are as many reasons to catch an exception at a lower level as
there are programs. (i.e. I can't think of all reasons, but there are
many.)

One is logging the exception.

Stuffing an inner exception into a newly thrown one isn't always the
most elegant solution. Logging is a case in point--I may want to note
the exception and simply pass it on up. I wouldn't want to wrap it in
another exception just because I logged it.
danielshanyf...@yahoo.com wrote:
Gentlemen,

There seem to be a lot of experts replying to this thread so I will add my own question. If you wanted to get the same exception at a higher
level, why would you catch it at all?

for example (modifying the previous code):

using System;

class Test
{
static void Main()
{
try
{
DoSomething();
}
//handle all exceptions without bias on moral behavior
catch (Exception e)
{
Console.WriteLine (e);
}
}

static void DoSomething()
{
try
{
DoSomethingNaughty();
}
catch (NaughtyException)
{
//handle the offending problem
}
//let all of the less ethicly challenged exceptions bubble up
the the calling Main()
}

static void DoSomethingNaughty()
{
string x = null;
bool y = x.Equals("hello");
}
}

If I wanted to do some intermediate processing in the routine and then still post an error to the calling method, I would throw a new
exception (of whatever type) with the original exception as the inner
exception. Does stuffing the old exception into the new still mess up the stack?

Thanks,
Dan


Nov 16 '05 #11
GoogleNewsReaderMan,
Isn't that a matter of perspective?

The stack trace below(above?) DoSomething is preserved.

The stack trace within DoSomething is the line that threw the exception (The
throw itself).

The stack trace above(below?) DoSomething is maintained.

Although I can see the flip side also, if there were multiple calls to
DoSomethingNaughty within DoSomething you would not be certain which call
threw the original exception...

Interesting I never specifically noticed that throw by itself modified the
entry for the routine it was in...

I'm curious what VB.NET's When clause does to the stack trace now...

Thanks for the info
Jay

"GoogleNewsReaderMan" <lo***********@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
One arguable flaw here, though, is that you lose the location within
the DoSomething() method --- the line number is that of the throw. So,
in fact, the stack trace is modified, and not that of the original
exception.
Jon wrote:
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
> the rethrow does not preserve the stack. it is not a bug. You need preserve > the stack and then throw a new exception with the amended stack


Not true. Try this:

using System;

class Test
{
static void Main()
{
try
{
DoSomething();
}
catch (Exception e)
{
Console.WriteLine (e);
}
}

static void DoSomething()
{
try
{
DoSomethingNaughty();
}
catch (Exception)
{
throw;
}
}

static void DoSomethingNaughty()
{
string x = null;
bool y = x.Equals("hello");
}
}

Compile it with debug turned on and run it. I get a stack trace of:
System.NullReferenceException: Object reference not set to an

instance
of an object.
at Test.DoSomethingNaughty() in c:\test\Test.cs:line 32
at Test.DoSomething() in c:\test\Test.cs:line 25
at Test.Main() in c:\test\Test.cs:line 9

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #12
> GoogleNewsReaderMan,
Isn't that a matter of perspective?
Yes, that's why I said it was "arguable." Technically, the stack trace
is correct because the last location was that of the throw. But it
isn't what I want in all circumstances.
Although I can see the flip side also, if there were multiple calls to DoSomethingNaughty within DoSomething you would not be certain which call threw the original exception...
Precisely!
Jay B. Harlow [MVP - Outlook] wrote: GoogleNewsReaderMan,
Isn't that a matter of perspective?

The stack trace below(above?) DoSomething is preserved.

The stack trace within DoSomething is the line that threw the exception (The throw itself).

The stack trace above(below?) DoSomething is maintained.

Although I can see the flip side also, if there were multiple calls to DoSomethingNaughty within DoSomething you would not be certain which call threw the original exception...

Interesting I never specifically noticed that throw by itself modified the entry for the routine it was in...

I'm curious what VB.NET's When clause does to the stack trace now...

Thanks for the info
Jay

"GoogleNewsReaderMan" <lo***********@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
One arguable flaw here, though, is that you lose the location within the DoSomething() method --- the line number is that of the throw. So, in fact, the stack trace is modified, and not that of the original
exception.
Jon wrote:
<"Alvin Bruney [MVP]" <vapor at steaming post office>> wrote:
> the rethrow does not preserve the stack. it is not a bug. You need
preserve
> the stack and then throw a new exception with the amended stack

Not true. Try this:

using System;

class Test
{
static void Main()
{
try
{
DoSomething();
}
catch (Exception e)
{
Console.WriteLine (e);
}
}

static void DoSomething()
{
try
{
DoSomethingNaughty();
}
catch (Exception)
{
throw;
}
}

static void DoSomethingNaughty()
{
string x = null;
bool y = x.Equals("hello");
}
}

Compile it with debug turned on and run it. I get a stack trace

of: System.NullReferenceException: Object reference not set to an

instance
of an object.
at Test.DoSomethingNaughty() in c:\test\Test.cs:line 32
at Test.DoSomething() in c:\test\Test.cs:line 25
at Test.Main() in c:\test\Test.cs:line 9

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #13

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

Similar topics

8
by: Christian Schuhegger | last post by:
hi, we are working on a c++ project which has a lot of thow list specifications which in the end causes a lot of problems. now i would like to remove them from the project. because the project...
3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
1
by: Rennie deGraaf | last post by:
I want to use throw() specifiers on some of my methods while debugging, to help me keep track of what is going on, but remove them in release builds. My method of doing this is #ifdef DEBUG...
9
by: wij | last post by:
Hi: In recent pondering, I am begining suspecting the usefulness of throw specification in the language(I know this opinion have long existed), even for delete!. The primary reason is that...
3
by: Nenad Dobrilovic | last post by:
Hi, I have function which is throwning exception, like this: public class LoggedException : Exception { public void Throw() { throw this; } }
6
by: Arjen | last post by:
Hi, I'm reading the enterprise library documentation and there I see the throw statement. try { // run code } catch(Exception ex) {
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
2
by: =?Utf-8?B?UHVyZSBIZWFydA==?= | last post by:
Hi for me the throw statment is very confusing and un clear, i mean for structured error handeling if the eception is not handeled it will search for handlers in the calling methods hirarchy so...
6
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include...
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
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...
1
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...
0
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,...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.