473,465 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Question about throw;

From the book by Jeffrey Richter:
throw; will not change the origin of the exception, whereas
throw ex; will change the origin of the exception to this statement.

When I try the following program, I get this result:

3: at ... in c:\dev\vs.net\exceptiontest3\form1.cs:line 91
2: at ... in c:\dev\vs.net\exceptiontest3\form1.cs:line 101
1: at ... in c:\dev\vs.net\exceptiontest3\form1.cs:line 101

name of method omitted for shorter lines.

It looks to me as the following happens:
- if the exception is thrown in the try-block, the catch-block will
change the origin when rethrowing
- if the exception is thrown in a nested scope (method call), the catch-
block will not change the origin when rethrowing

If the throw statement is moved to a new method and that method is called
in place of the existing throw statement, the exception origin is not
changed.

Is this correct ? Is there something I've misunderstood here ? Done wrong
?

The reason I'm looking into this is that I'm trying to build a bug
reporting class that will dispatch bug reports into a database for later
handling. I was considering using the line number to distinguish
different exceptions from each other, but if the location I decide to
report it might make the exception be reported twice, then I'll have to
rethink that idea.

Here's the code, simply paste this into an empty winforms app, add a
button and let the event handler call TestMethod1(); The line numbers
might of course vary from what I got but you should see the same effect.

private void TestMethod3()
{
try
{
throw new Exception("Test");
}
catch (Exception ex)
{
foreach (String line in ex.StackTrace.Split('\r', '\n'))
if (line.IndexOf(" in ") > 0)
{
System.Diagnostics.Debug.WriteLine("3: " + line.Trim());
break;
}
throw;
}
}

private void TestMethod2()
{
try
{
TestMethod3();
}
catch (Exception ex)
{
foreach (String line in ex.StackTrace.Split('\r', '\n'))
if (line.IndexOf(" in ") > 0)
{
System.Diagnostics.Debug.WriteLine("2: " + line.Trim());
break;
}
throw;
}
}

private void TestMethod1()
{
try
{
TestMethod2();
}
catch (Exception ex)
{
foreach (String line in ex.StackTrace.Split('\r', '\n'))
if (line.IndexOf(" in ") > 0)
{
System.Diagnostics.Debug.WriteLine("1: " + line.Trim());
break;
}
throw;
}
}
Nov 16 '05 #1
0 1135

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

Similar topics

12
by: Mark | last post by:
Apologies for the lengthy posting.... In a previous posting:...
5
by: Kevin Jackson | last post by:
In the following code snippet, will the finally block be executed when the throw is executed in the catch block???? I'm assuming it will. catch (Exception e) { // if...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
5
by: Gelios | last post by:
Hello All! I am going to crazy and feeling myself so stupid but I don't understand such behaviour. I have code: public int getNextAgentId() { Int32 agent_id = 0; IDataReader dr =...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
3
by: xllx.relient.xllx | last post by:
I have a question about an example presented in the book "INFORMIT C++ Reference Guide" by Danny Kalev: <code> string getmagicword() ( return string("Supercalifragilisticexpialidocious"); );
3
by: jjsavage | last post by:
Hi again, Ok, so the compiler tells me I need two GetEnumerator() methods for my class implementing IList<string>: one returning an IEnumerator, and the other returning IEnumerator<string>. ...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
1
by: erizy | last post by:
There are situations that exception could be throw within constructor and we usually employ auto_ptr to protect resources. But if we do not have auto_ptr can we use the following technique to handle...
5
by: Vijay | last post by:
Hi All, I am not able to figure out what exactly happening in below code. what is control flow. Can anyone clear my confusion? Code: class A { public: A(){cout<<"In Constructor\n";}
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:
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
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,...
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
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...
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
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: 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.