Why can't a 'continue' statement be inside a 'finally' block?

后端 未结 11 766
情深已故
情深已故 2020-12-23 02:38

I don\'t have a problem; I\'m just curious. Imagine the following scenario:

foreach (var foo in list)
{
    try
    {
         //Some code
    }
    catch (E         


        
11条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 03:13

    You may think it makes sense, but it doesn't make sense actually.

    foreach (var v in List)
    {
        try
        {
            //Some code
        }
        catch (Exception)
        {
            //Some more code
            break; or return;
        }
        finally
        {
            continue;
        }
    }
    

    What do you intend to do a break or a continue when an exception is thrown? The C# compiler team doesn't want to make decision on their own by assuming break or continue. Instead, they decided to complain the developer situation will be ambiguous to transfer control from finally block.

    So it is the job of developer to clearly state what he intends to do rather than compiler assuming something else.

    I hope you understand why this doesn't compile!

提交回复
热议问题