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();
        }
    }
}
6
  • No I'm asking about an exception in the inner finally block @BenReich Commented May 30, 2013 at 15:00
  • And no, @Anirudh, I'm asking about a nested one. Commented May 30, 2013 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 Commented May 30, 2013 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. Commented May 30, 2013 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.. Commented May 30, 2013 at 15:11

1 Answer 1

-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 :

Sign up to request clarification or add additional context in comments.

1 Comment

How is the outer Try Cartch going to read the exception if the exception happens in a finally??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.