C# statements within finally
-blocks are always executed. In this way, the finally
-keyword helps ensure the correct execution of C# programs.
This keyword ensures a block of statements are always reached before the enclosing method is exited. Though it requires no exception to occur, it is part of the exception handling control flow.
This program shows how the finally
-clause is part of the control flow in programs. In this program, a random number is generated.
finally
-block is reached immediately after processing completes.finally
-block can be used to ensure that some logic is always executed before the method is exited.string
"Control flow reaches finally" is printed to the console window.using System; class Program { static void Main() { try { // Acquire random integer for use in control flow. // ... If the number is 0, an error occurs. // ... If 1, the method returns. // ... Otherwise, fall through to end. int random = new Random().Next(0, 3); // 0, 1, 2 if (random == 0) { throw new Exception("Random = 0"); } if (random == 1) { Console.WriteLine("Random = 1"); return; } Console.WriteLine("Random = 2"); } finally { // This statement is executed before Main exits. // ... It is reached when an exception is thrown. // ... It is reached after the return. // ... It is reached in other cases. Console.WriteLine("Control flow reaches finally"); } } }Unhandled Exception: System.Exception: Random = 0 at Program.Main() ... Control flow reaches finallyRandom = 1 Control flow reaches finallyRandom = 2 Control flow reaches finally
A try block must come before finally. We cannot put a finally in a block all by itself (a prelude part, the "try" is always first).
class Program { static void Main() { // Try must come before finally. finally { } } }Error CS1003 Syntax error, 'try' expected Error CS1514 { expected Error CS1513 } expected
We cannot place 2 finally
-blocks in an exception handling construct. Unlike "try" only 1 finally is allowed—a syntax error is reported if 2 finally
-blocks are used.
class Program { static void Main() { // Only one finally is allowed. try { string value = "HELLO"; } finally { } finally { } } }Error CS1003 Syntax error, 'try' expected Error CS1514 { expected Error CS1513 } expected
Console
programOne use for the finally
-clause in C# programs is to wrap the body of the Main
method with a try-finally
construct.
finally
-clause, you can print a message that indicates that the program is finished processing.Main()
.using System; class Program { static void Main() { try { Console.WriteLine("Start"); // Perform important computations in try block. string temp = "x"; for (int i = 0; i < 1000; i++) { temp += "x"; } } finally { // Notify user when program is done. Console.WriteLine("Finished"); } } }Start Finished
How does the catch block relate to the finally
-block? The two constructs are separate. The catch block is for handling errors.
finally
-block can be used to perform resource management tasks such as closing files.finally
-block does not involve an error. But it relies on the same alternate control flow mechanism as catch.using System; // Create an exception, and then catch it and use finally. try { Console.WriteLine("TRY"); throw new Exception("?"); } catch (Exception ex) { Console.WriteLine("CATCH"); } finally { Console.WriteLine("FINALLY"); }TRY CATCH FINALLY
A finally
-block is a way to ensure a piece of logic is executed before the method is exited. The finally
-construct is separate conceptually from the catch block.