0

So I was wondering about the behaviour of a nested try-catch-finally blocks.

What I mean is, what if inside the first finally block, we have another try-catch-finally blocks and a exception happens in the inner finally block??

Is the exception going to be propagated? And is it going to be caught somewhere?

Where should I catch the exception? In the inner finally block or if it's propagated should I catch it from the upper code?

Example:

static bool Func()
{
    try
    {}
    catch
    {}
    finally
    {
        try
        {}
        catch
        {}
        finally
        {
           throw new ApplicationException();
        }
    }
}
AAlferez
  • 1,480
  • 1
  • 22
  • 48
  • No I'm asking about an exception in the inner finally block @BenReich – AAlferez May 30 '13 at 15:00
  • And no, @Anirudh, I'm asking about a nested one. – AAlferez May 30 '13 at 15:01
  • the inner finally block's exception would propagate out of the containing finally block and would be handled at the higher level..So excetion thrown in inner finally block would go outward until it finds a specific catch block at higher level or it would throw the exception if none is found – Anirudha May 30 '13 at 15:07
  • You could easily write code and test your hypothesis any way you want. This question is rightfully closed, there are plenty of resources on this site and elsewhere that describe the behavior, and you can test to discover and confirm those behaviors as well. Do more research. – Anthony Pegram May 30 '13 at 15:09
  • as i said for your given example there's no catch block for that exception and so it would not be catch'ed..remember all other outer finally block would not be executed further.. – Anirudha May 30 '13 at 15:11
  • So or I put a try with a catch inside for the exception or I put a try for all the code and the catch under – AAlferez May 30 '13 at 15:16

1 Answers1

-1

Depends. You can handle the exception in your inner exception, but if you don't want to handle it, you can just THROW it so the outer Try Catch will 'receive' the exception.

See here for more detailed example :

Community
  • 1
  • 1
Swift
  • 7
  • 2