473,549 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ 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\e xceptiontest3\f orm1.cs:line 91
2: at ... in c:\dev\vs.net\e xceptiontest3\f orm1.cs:line 101
1: at ... in c:\dev\vs.net\e xceptiontest3\f orm1.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.S plit('\r', '\n'))
if (line.IndexOf(" in ") > 0)
{
System.Diagnost ics.Debug.Write Line("3: " + line.Trim());
break;
}
throw;
}
}

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

private void TestMethod1()
{
try
{
TestMethod2();
}
catch (Exception ex)
{
foreach (String line in ex.StackTrace.S plit('\r', '\n'))
if (line.IndexOf(" in ") > 0)
{
System.Diagnost ics.Debug.Write Line("1: " + line.Trim());
break;
}
throw;
}
}
Nov 16 '05 #1
0 1142

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

Similar topics

12
1419
by: Mark | last post by:
Apologies for the lengthy posting.... In a previous posting: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=3fb3728b%240%2422605%24cc9e4d1f%40news.dial.pipex.com&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D3fb3728b%25240%252422605%2524cc9e4d1f%2540news.dial.pipex.com I asked a question about...
5
1867
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 (ContextUtil.IsInTransaction)ContextUtil.SetAbort(); ReportError objError = new ReportError(); ...
44
4164
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 user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box,...
5
7003
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 = dbap.DBDataReader("SELECT MAX(agent_id) FROM Agents"); if(dr.Read()) { agent_id = dr.GetInt32(0);
14
1816
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 ????? OK, this function may return a boolean, and if this is true, then no message back is really required, but if it fails then some supporting message...
3
1782
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
4514
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>. Problem is, these methods both take no parameters, so of course the compiler complains that it can't tell the difference. But when I only implement one...
7
4458
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 to be checking that the correct data type is used DataAcess sets an abstract class and methods
1
1712
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 it? Please search "QUESTION" and see my question. Thank you for helping!!! #include <cstdio> using namespace std; class E1 { public:...
5
1532
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";}
0
7532
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...
0
7971
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...
1
7491
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...
0
7823
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6055
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...
0
5101
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1068
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
776
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.