Finally....another exception?

A

Alex

Hi.
What would happen if an exception occurs inside a Finally block and at the
same time inside the try another exception was thrown without been handled
by any catch?

Alejandro.
 
D

David Levine

It would throw the new exception and the original exception would not be
seen. Given the code...

try
{
throw new Exception("1st exception");
}
catch(Exception ex1)
{
// catches 1st exception
}
finally
{ // runs after the catch block catches the 1st exception
throw new Exception("2nd exception");
}

the 2nd exception is not caught in this snippet, and unless it gets caught
somewhere further back up the call stack it would eventually result in an
unhandled exception.

If the entire snippet were enclosed in a surrounding try-catch block, then
the outermost catch would catch the 2nd exception - the 1st exception gets
dropped.
 
A

Alex

Thanks. Here is the situation I was thinking about:

try
{
throw new Exception("1st exception");
}
catch(AnotherException ex1)
{
// DON'T catches 1st exception
}
finally
{ // runs after the catch block DON'Tcatches the 1st exception
throw new Exception("2nd exception");
}

Alejandro.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top