473,769 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rethrowing does not work?

I do not understand something. A caught exception stack points to an
argument-free 'throw' in a catch block. Meantime, it is widely known that
these statements re-throw the original exceptions. I expected, therefore,
that the stack points to the original 'throw e' code line.
Jun 9 '07 #1
13 1897
valentin,

Nope, it does not. If you rethrow the original exception with "throw
e", then the point from which you threw the exception changed, resetting the
call stack on the exception.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"valentin tihomirov" <V_*********@be st.eewrote in message
news:Ow******** ******@TK2MSFTN GP02.phx.gbl...
>I do not understand something. A caught exception stack points to an
argument-free 'throw' in a catch block. Meantime, it is widely known that
these statements re-throw the original exceptions. I expected, therefore,
that the stack points to the original 'throw e' code line.
Jun 9 '07 #2
Oh, excuse me. I had rewise my data. It seems that both 'throw' and 'throw
e' rethrow
http://msdn2.microsoft.com/en-us/lib...sw(VS.71).aspx But my
previous data told that 'throw', given a throwable object, raises a new
exception. And that is the difference. The 2nd google match on "c# rethrow"
affirms this http://bartdesmet.net/blogs/bart/arc...3/12/3815.aspx

Anyway, I do not understand why re-throw overwrites the line of code where
exception was raised by the line where it was re-thrown. My rationale is
that the call stack makes value pointing to the cause of the exception
rather than the last place where it was handeled.
Jun 9 '07 #3
valentin tihomirov <V_*********@be st.eewrote:
I do not understand something. A caught exception stack points to an
argument-free 'throw' in a catch block. Meantime, it is widely known that
these statements re-throw the original exceptions. I expected, therefore,
that the stack points to the original 'throw e' code line.
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 9 '07 #4
I'm sure it is pretty obvious. But if you like

using System; // line 1

class Rerise {

public static void Main(string[] args) {

try {
throw new Exception("Can I escape?"); // line 8
} catch (Exception e) {
Console.WriteLi ne("Logged error: " + e.Message);
throw e; // line 11
}
}
}

Compile it with /debug key and see that stack trace will point to 'line 11'
where it is rethrown rather than to 'line 8' where it originates.
Jun 9 '07 #5
As it should be. The spec is pretty clear on this, when you throw an
exception, the stack trace originates from the point of where it is thrown,
except when there is no exception stated with the throw statement, in which
case, the original exception is allowed to bubble up without having its
stack trace modified.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"valentin tihomirov" <V_*********@be st.eewrote in message
news:uM******** ******@TK2MSFTN GP03.phx.gbl...
I'm sure it is pretty obvious. But if you like

using System; // line 1

class Rerise {

public static void Main(string[] args) {

try {
throw new Exception("Can I escape?"); // line 8
} catch (Exception e) {
Console.WriteLi ne("Logged error: " + e.Message);
throw e; // line 11
}
}
}

Compile it with /debug key and see that stack trace will point to 'line
11' where it is rethrown rather than to 'line 8' where it originates.
Jun 9 '07 #6

"valentin tihomirov" <V_*********@be st.eewrote in message
news:uM******** ******@TK2MSFTN GP03.phx.gbl...
I'm sure it is pretty obvious. But if you like

using System; // line 1

class Rerise {

public static void Main(string[] args) {

try {
throw new Exception("Can I escape?"); // line 8
} catch (Exception e) {
Console.WriteLi ne("Logged error: " + e.Message);
throw e; // line 11
}
}
}

Compile it with /debug key and see that stack trace will point to 'line
11' where it is rethrown rather than to 'line 8' where it originates.
That is a throw, not a rethrow.

Change the line to "throw;" if you want the call stack preserved.
>
Jun 9 '07 #7
As it should be. The spec is pretty clear on this, when you throw an
exception, the stack trace originates from the point of where it is
thrown, except when there is no exception stated with the throw statement,
in which case, the original exception is allowed to bubble up without
having its stack trace modified.
Why then it should not "bubble up without having its stack trace modified"
if it is allowed? Why it overwirtes the stack with the rethrow location?
Hiding the exception reason from user defeats the whole value of stack
trace.
Jun 9 '07 #8

"valentin tihomirov" <V_*********@be st.eewrote in message
news:uL******** ******@TK2MSFTN GP06.phx.gbl...
Oh, excuse me. I had rewise my data. It seems that both 'throw' and 'throw
e' rethrow
http://msdn2.microsoft.com/en-us/lib...sw(VS.71).aspx But my
Looks like a documentation error to me.
previous data told that 'throw', given a throwable object, raises a new
exception. And that is the difference. The 2nd google match on "c#
rethrow" affirms this
http://bartdesmet.net/blogs/bart/arc...3/12/3815.aspx

Anyway, I do not understand why re-throw overwrites the line of code where
exception was raised by the line where it was re-thrown. My rationale is
that the call stack makes value pointing to the cause of the exception
rather than the last place where it was handeled.
Jun 9 '07 #9

"valentin tihomirov" <V_*********@be st.eewrote in message
news:eu******** ******@TK2MSFTN GP04.phx.gbl...
> As it should be. The spec is pretty clear on this, when you throw an
exception, the stack trace originates from the point of where it is
thrown, except when there is no exception stated with the throw
statement, in which case, the original exception is allowed to bubble up
without having its stack trace modified.

Why then it should not "bubble up without having its stack trace modified"
if it is allowed? Why it overwirtes the stack with the rethrow location?
Hiding the exception reason from user defeats the whole value of stack
trace.
"throw e" performs certain steps, including setting up a stack trace.
Doesn't matter if e was thrown before, it makes a new stack trace. This
allows you to do things like preallocate or reuse exception objects.

Also, some libraries might want to hide where an exception actually
occurred, because the programmers feel that threatens their intellectual
property. Seems ridiculous to you and me, but some people think that way.

Jun 9 '07 #10

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

Similar topics

7
4859
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As InternetExplorer But then I cant see when a user exit my objIExplorer and than an error will show up when I try to open a link in the IE window that does not exist... What can I do about this and way does it not work in win 98?
2
1723
by: Martin Smith | last post by:
If a class has no need for any specific clean up code in a particular catch block is there any benefit to catching and rethrowing exceptions, rather than just letting the original exception bubble up to the calling function If so what?
6
1676
by: Chris Newcombe | last post by:
Please could someone on the VC++ 7.0 compiler team (note; not 7.1) tell me if this code is handled 'correctly' (i.e. as the original poster suggests) in all cases? http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=89ed5u%248ck%40library1.airnews.net (Incase the URL doesn't make it I've copied it below.) I have a situation where I really need this technique. But the fact that Dave Abrahams wasn't sure about it very much worries...
2
4390
by: Lasse Vågsæther Karlsen | last post by:
If I got the following code: try { // something that might throw an exception } catch (Exception ex) { // Log contents of ex here throw;
14
4862
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it can be done using the designer but I intentionally don't want to use that. The one reason is that you cannot change the code generated by the designer. The other could be that you have more free hand and control to design your GUI. 2....
5
2863
by: Steven T. Hatton | last post by:
I haven't thought about rethrowing an exception in a long time, and tried to do it the wrong way. Now I'm curious about /why/ it's wrong. My expectation was that the exception instance would simply be passed up the call stack as the same object. It would therefore behave polymorphically. Is the behavior shown below compiler-specific? I'm using gcc 4.0.2. In terms of what goes on at runtime, why does the `throw e' act as if I threw a...
15
1900
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
In my opinion rethrowing exceptions without providing anny extra information is a totall waste Examples : in my opinion wrong : A: Public sub DoSomeStuff() Try do it
4
2374
by: Coder Guy | last post by:
I am implementing an asynchronous handler (similar to Control.EndInvoke) and if the other thread throws an exception, I catch it and rethrow it on the current thread. How can I rethrow the exception on the current thread while maintaining the stack trace? The best I've come up with is encapsulating the exception as an inner exception. I would prefer to just rethrow the exception directly -- no need for an inner exception. How does...
5
6468
by: jerseycat10 | last post by:
I am trying to overcome the following hurdle. We have several exception types, derived from a "BaseException" type. Lets say SpecialException inherits from BaseException. Lets say a SpecialException is thrown, and handled by try/catch that is catching "BaseException". If I retry to throw this exception, I get a core dump.
0
9579
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10208
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10038
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9987
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8867
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5294
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3952
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
2
3558
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.