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

C# throw oddity

These two fragments do not act identically:

(1)
catch(Exception ex)
{
...
throw ex;
}

(2)
catch (Exception ex)
{
...
throw;
}

In (1), ex.StackTrace is reinitialized, so the exception will appear to have
originated inside the fragment. That is, the MSIL throw instruction is
generated.

In (2) ex.StackTrace is not reinitialized, so the exception will appear to
have originated wherever it was last thrown. That is, the MSIL rethrow
instruction is generated.

I couldn't find any place this is documented.
Nov 15 '05 #1
2 2922
Mike Schilling <ms*************@hotmail.com> wrote:
These two fragments do not act identically:
<snip>

Indeed they don't.
I couldn't find any place this is documented.


No - it's not in the spec as far as I can see. Indeed, I'd go so far as
to say that the spec *implies* that they should do the same thing. From
section 15.9.5 (ECMA):

<quote>
A throw statement with an expression throws the value produced by
evaluating the expression. The expression must denote a value of the
class type System.Exception or of a class type that derives from
System.Exception. If evaluation of the expression produces null, a
System.NullReferenceException is thrown instead.

A throw statement with no expression can be used only in a catch block,
in which case, that statement re-throws the exception that is currently
being handled by that catch block.
</quote>

I suppose there's a difference between "re-throws" and "throws", but
it's a bit tricksy. I think it should at the very least suggest that
there *can* be a difference, and perhaps give an informational note
giving an example of what the difference is in the MS CLR.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
Hi Mike, Jon,

<quote>
A throw statement with an expression THROWS the value produced by
evaluating the expression. The expression must denote a value of the
class type System.Exception or of a class type that derives from
System.Exception. If evaluation of the expression produces null, a
System.NullReferenceException is thrown instead.

A throw statement with no expression can be used only in a catch
block,
in which case, that statement RE-throws the exception that is
currently
being handled by that catch block.
</quote>

If I'd read that before reading Mike's post (thanks Mike, learned
something new there), I'd probably have missed the sublety or assumed
a typo or something. Having read Mike's post <first>, it makes perfect
sense.

I think they should introduce a new keyword, "rethrow", to make it
explicit.

Regards,
Fergus
Nov 15 '05 #3

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

Similar topics

12
by: Michael Foord | last post by:
Here's a little oddity with 'print' being a reserved word... >>> class thing: pass >>> something = thing() >>> something.print = 3 SyntaxError: invalid syntax >>> print something.__dict__...
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? ...
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) {
5
by: jmdocherty | last post by:
All, I've been trying to set up a CSS layout and all seems well in Firefox but in Safari it just seems to be plain weird! I hope someone can help tell me whether this is a problem with my code...
43
by: michael.f.ellis | last post by:
The following script puzzles me. It creates two nested lists that compare identically. After identical element assignments, the lists are different. In one case, a single element is replaced. In...
6
by: Rex the Strange | last post by:
Hello all, Traditionally I'm a Delphi and VB6 programmer and have recently started working with VB.NET (which, I might add, I love, but that's beside the point). Anyway, I was trying to make a...
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.