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

后端 未结 11 784
情深已故
情深已故 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:07

    Here is a reliable source:

    A continue statement cannot exit a finally block (Section 8.10). When a continue statement occurs within a finally block, the target of the continue statement must be within the same finally block; otherwise, a compile-time error occurs.

    It is taken from MSDN, 8.9.2 The continue statement.

    The documentation say that:

    The statements of a finally block are always executed when control leaves a try statement. This is true whether the control transfer occurs as a result of normal execution, as a result of executing a break, continue, goto, or return statement, or as a result of propagating an exception out of the try statement. If an exception is thrown during execution of a finally block, the exception is propagated to the next enclosing try statement. If another exception was in the process of being propagated, that exception is lost. The process of propagating an exception is discussed further in the description of the throw statement (Section 8.9.5).

    It is from here 8.10 The try statement.

提交回复
热议问题