Finally. 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.
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
Finally, syntax error. 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
Finally, syntax 2. 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 program. 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.
Tip Additional flags can be used to report status messages in finally. Or multiple blocks can be placed in 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
Catch. How does the catch block relate to the finally block? The two constructs are separate. The catch block is for handling errors.
Important The finally block can be used to perform resource management tasks such as closing files.
Tip The 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.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 9, 2025 (new example).