Search code examples
c#try-catchtry-catch-finally

Try/Catch within loop in C#


There is a try/catch/finally block within the for or foreach loop.

What will happen if there is a break statement within the try block?

Will finally block be executed?


Solution

  • Yes, finally blocks hit even if you have a jump statement such as break.

    Typically, the statements of a finally block run when control leaves a try statement.The transfer of control can occur as a result of normal execution, of execution of a break, continue, goto, or return statement, or of propagation of an exception out of the try statement.

    From https://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx