X hits on this document

1355 views

0 shares

0 downloads

0 comments

191 / 396

C# LANGUAGE SPECIFICATION

class Test { static void Main() { while (true) { try { try { Console.WriteLine("Before break"); break; } finally { Console.WriteLine("Innermost finally block"); } } finally { Console.WriteLine("Outermost finally block"); } } Console.WriteLine("After break"); } }

the finally blocks associated with two try statements are executed before control is transferred to the target of the jump statement.

The output produced is as follows:

Before break Innermost finally block Outermost finally block After break

8.9.1 The break statement

The break statement exits the nearest enclosing switch, while, do, for, or foreach statement.

break-statement: break   ;

The target of a break statement is the end point of the nearest enclosing switch, while, do, for, or foreach statement. If a break statement is not enclosed by a switch, while, do, for, or foreach statement, a compile-time error occurs.

When multiple switch, while, do, for, or foreach statements are nested within each other, a break statement applies only to the innermost statement. To transfer control across multiple nesting levels, a goto statement (§‎8.9.3) must be used.

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

A break statement is executed as follows:

If the break statement exits one or more try blocks with associated finally blocks, control is initially transferred to the finally block of the innermost try statement. When and if control reaches the end point of a finally block, control is transferred to the finally block of the next enclosing try statement. This process is repeated until the finally blocks of all intervening try statements have been executed.

Control is transferred to the target of the break statement.

Because a break statement unconditionally transfers control elsewhere, the end point of a break statement is never reachable.

178Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1355
Page views1355
Page last viewedSun Jan 22 02:24:14 UTC 2017
Pages396
Paragraphs9401
Words133190

Comments