Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking out of a for loop from a catch block

Tags:

c#

Is finally executed in this case? I wrote this code and couldn't decide if finally is really executed in this case or not. I would really like some explanations no matter what the answer is.

foreach(string s in allStrings)
{
    try
    {
        //Error happens here
    }
    catch(Exception ex)
    {
        //Handle exception
        break;
    }
    finally
    {
        //Clean up code
    }
}
like image 985
iJK Avatar asked Nov 10 '10 23:11

iJK


2 Answers

You've written 90% of the code that you'd need to answer this question for yourself.

Keep writing.

like image 119
Matt Ball Avatar answered Sep 22 '22 09:09

Matt Ball


Yes. Finally blocks are always executed when control leaves a corresponding try or catch block. (Unless something super-special happens, like a runtime crash or the thread was aborted.)

like image 44
cdhowie Avatar answered Sep 22 '22 09:09

cdhowie