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

Exception propogation question

Consider the following code:

try
{
methodA();

try
{
methodB();
}
finally
{
methodC();
}
}
catch (Exception e)
{
//some sort of error handling
}

If methodA completes successfully, and methodB throws an exception, methodC
would then be called before the catch statement correct?

What happens if methodC then throws an exception? Which of those exceptions
get's handled in the catch? Is the other exception 'lost'?

--
Adam Clauss

Jan 25 '07 #1
1 1147
Hi Adam,

A simple test reveals that the exception thrown by MethodC replaces the
previous exception thrown by MethodB, and its the MethodC exception that is
caught by the outer try...catch block:

static void Main(string[] args)
{
try
{
MethodA();

try
{
MethodB();
}
finally
{
MethodC();
}
}
catch (Exception ex)
{
Console.WriteLine("Catch: " + ex.Message);
}
}

static void MethodA()
{
Console.WriteLine("MethodA");
}

static void MethodB()
{
Console.WriteLine("MethodB");
throw new Exception("Exception from MethodB");
}

static void MethodC()
{
Console.WriteLine("MethodC");
throw new Exception("Exception from MethodC");
}

Result:

MethodA
MethodB
MethodC
Catch: Exception from MethodC

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"Adam Clauss" <ca*****@tamu.eduwrote in message
news:12*************@corp.supernews.com...
Consider the following code:

try
{
methodA();

try
{
methodB();
}
finally
{
methodC();
}
}
catch (Exception e)
{
//some sort of error handling
}

If methodA completes successfully, and methodB throws an exception,
methodC would then be called before the catch statement correct?

What happens if methodC then throws an exception? Which of those
exceptions get's handled in the catch? Is the other exception 'lost'?

--
Adam Clauss

Jan 25 '07 #2

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

Similar topics

6
by: Gonçalo Rodrigues | last post by:
Hi, For error processing I found convenient maintaining a dictionary where the keys are exception *classes* and the values are callables. Of course, for this to work, exception classes have to...
12
by: Ori | last post by:
Hi, I have a simple question. Let say that i have 3 functions as following private main() { Object oTop = new Object(); }
12
by: Bret Pehrson | last post by:
Suppose the following: // Unmanaged code class UnmanagedException /* not visible outside of unmanaged code */ { }; void DoSomething() /* visible (exported) to managed code */ { throw new...
10
by: tony | last post by:
Hello!! As you know every user defined exception must be derived from class Exception. Now to my question if I write catch then every exception will be caught. If I instead write...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
5
by: Bry | last post by:
I've created a class that offers an enhanced way of handling fatal exceptions. The class allows the user to optionaly submit a http based anonymous error report to myself, and also records details...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
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";}
9
by: =?Utf-8?B?UmFq?= | last post by:
How do I know which methods will throw exception when I am using FCL or other third party .Net library? I am developer of mostly native Windows applications and now .Net. After working few...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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,...
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.