C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: This random value is used to determine whether to throw an exception, immediately return, or do nothing.
ReturnInfo: In each of the 3 cases, the finally block is reached immediately after processing completes.
Thus: The finally block can be used to ensure that some logic is always executed before the method is exited.
Important: In all cases, the string "Control flow reaches finally" is printed to the console window.
ConsoleC# program that uses finally and control flows
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");
}
}
}
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
C# program that shows finally syntax error
class Program
{
static void Main()
{
// Try must come before finally.
finally
{
}
}
}
Output
Error CS1003
Syntax error, 'try' expected
Error CS1514
{ expected
Error CS1513
} expected
C# program that shows try expected error
class Program
{
static void Main()
{
// Only one finally is allowed.
try
{
string value = "HELLO";
}
finally
{
}
finally
{
}
}
}
Output
Error CS1003
Syntax error, 'try' expected
Error CS1514
{ expected
Error CS1513
} expected
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().
C# program that uses try, finally in console
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");
}
}
}
Output
Start
Finished
Info: Each method contains an exception-handling section that indicates where the finally handler is defined.
Then: When the leave instruction is encountered, that section is used to direct the flow of control.
Also: The IL also defines the endfinally instruction. This is an implementation detail.
Goto: For example, the goto statement has many restrictions and you cannot go to a label outside of the finally block.
GotoError: If you use a goto in a finally block, you will get an error: "Control cannot leave the body of a finally clause."
Meanwhile: The finally block can be used to perform important 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.
Catch