By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,214 Members | 1,496 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,214 IT Pros & Developers. It's quick & easy.

Exception propogation question

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.