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

  • Thread starter Thread starter Hasani
  • Start date Start date
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.
 
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?
 
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
 
"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)
 
Back
Top