Code in finally blocks is always executed. Finally provides a construct for ensuring the correct execution of programs. The finally block serves to ensure a block of statements are always reached before the enclosing method is exited. The statements are run after the exceptions and returns from the try block are processed.


This program demonstrates how the finally clause in the C# language is part of the control flow in programs. In this program, a random number is generated and this value is used to determine whether to throw an exception, simply return immediately, or do nothing.
In each of the three cases, the finally block is reached immediately after processing completes. In this way, the finally block can be used to ensure that some logic is always executed before the method is exited.
Program that uses finally and control flows [C#]
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 the Main method is exited.
// ... 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");
}
}
}
Possible output #1
Unhandled Exception: System.Exception: Random = 0
at Program.Main() ...
Control flow reaches finally
Possible output #2
Random = 1
Control flow reaches finally
Possible output #3
Random = 2
Control flow reaches finally
Control flow and paths. The program introduces the Main entry point method, where control flow begins. Next, a Random number is generated and this is used to randomly throw an exception, return or continue. In all cases, the string "Control flow reaches finally" is printed to the console window.
An important instruction is the "leave" instruction, which is generated whenever the try block is exited from. Each method contains an exception-handling section that indicates where the finally handler is defined. When the leave instruction is encountered, that section is used to direct the flow of control. The IL also defines the endfinally instruction.

In the C# language specification, you will encounter many points regarding the finally block and how it conflicts with certain language constructs. For example, the goto statement has many restrictions and you cannot go to a label outside of the finally block. Although the specification details these restrictions at length, this article only touches on these points.
Error 1 Control cannot leave the body of a finally clause

One use for the finally clause in C# programs is to wrap the body of the Main method with a try/finally construct. Then, in the finally clause, you can print a message that indicates that the program is finished processing. For console programs, you can put a Console.ReadLine in the finally clause, providing an exit prompt to better inform the user.
Console.ReadLine Tips
How does the catch block relate to the finally block in the C# language? Conceptually, the two constructs are separate; the catch block is for handling errors, while the finally block can be used to perform important resource management tasks such as closing files. The finally block does not require or indicate an error, but relies on the same alternate control flow mechanism that the catch construct uses.
Catch Examples
The finally block in the C# language provides a way to ensure a piece of logic is executed before the method is exited completely. The finally construct is separate conceptually from the catch block, but uses the exception-handling control flow mechanism as well. The finally block is used to free certain system resources in libraries, or for custom cleanup mechanisms or messages.
Keywords Exception Handling