Is there anyway to catch both exceptions in the following example?

W

William Stacey

try
{
throw new ApplicationException("Exception1");
}
catch
{
throw new ApplicationException("Exception in try block.");
}
finally
{
throw new ApplicationException("Exception in finally.");
}

Exception thrown in try block. Exception caught in catch block. Catch
block throws new exception, however finally also wants to throw an
exception. dont think you want to throw another exception in finally.
 
N

Nicholas Paldino [.NET/C# MVP]

Hasani,

Yes, that is right, because the exception thrown in the finally section
is the one that takes precedence (it is the most recent one thrown).

What are you trying to do?
 
1

100

Hi Hasani,
Throwing exception from within finally block is really bad idea and should
be avoided. Actually, throwing exception from finally block masks the first
exception and may confuse your exception handling logic. If you have code
that might throw exception in finally block use designated try/catch block
for it and recover in the finally block.

B\rgds
100
 
H

Hasani

"What are you trying to do?"

The real question is, what am I going to do, and I could only determine this
from the answers to my question. Thx you for your answers.

Nicholas Paldino said:
Hasani,

Yes, that is right, because the exception thrown in the finally section
is the one that takes precedence (it is the most recent one thrown).

What are you trying to do?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hasani said:
http://www.skidmore.edu/~h_blackw/finally.gif
I can only catch the exception thrown in the finally code section.
even if I do
catch(InvalidOperationException)
 

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