Updated April 12, 2023
Introduction to C# finally
An error may be caused due to exception which ends the method that is running currently in programming, and that method would have opened a file or a network which needs to be closed with ending of the current method. In order to overcome such problems, we have a special reserved keyword in C# called Finally and this Finally block of code will be executed when the execution of try and catch block stops, regardless of what ever conditions causes the stop of execution and it does not matter if the execution of try block stops normally or stopes due to an exception.The Finally block of code is executed and releasing the resources that was engaged is the main aim of Finally block of code and it is written after try and catch block of code.
Syntax:
try
{
//Block of code
}
// this can be optional
catch
{
//Block of code
}
finally
{
//Block of code
}
Working of finally Keyword in C#
- Any resources allocated in the try block can be cleaned up by using Finally block of code and the Finally block of code runs regardless of the exception raised in the try block.
- The statements in the Finally block of code are executed when the control leaves the try block of code. The control is transferred as part of normal execution or execution of goto statement, break statement, continue statement or return statement.
- If there is exception handling, the finally block of code associated with it runs for sure and if there is no handling of exception, the running of finally block of code depends on the triggering of exception unwind operation which in turn depends on the set up of the computer.
- There cannot be more than one Finally block of code in the same program in C#.
- The control will not leave the finally block because there is no goto statement, break statement, continue statement or return statement in finally block.
Examples to Implement C# finally
Below are the examples of C# Finally:
Example #1
C# program to demonstrate the use of Finally block of code in a program.
Code:
using System;
//a class called program is defined
class program
{
// a method called ClassA() is defined
static void ClassA()
{
try
{
Console.WriteLine("We are inside class A");
//An exception is thrown
throw new Exception("An exception is thrown");
}
//finally block is executed regardless of the exception is handled or not
finally
{
Console.WriteLine("This is the finally block of Class A");
}
}
// a method called ClassB() is defined
static void ClassB()
{
try
{
Console.WriteLine("We are Inside class B");
return;
}
//finally block is executed regardless of the exception is handled or not
finally
{
Console.WriteLine("This is the finally block of class B");
}
}
// Main Method is called
public static void Main(String[] args)
{
try
{
ClassA();
}
catch (Exception)
{
Console.WriteLine("The exception that is thrown is caught");
}
ClassB();
}
}
Output:
Explanation: In the above program, the program is the class defined. Then a method called ClassA is defined in which try and finally blocks of code are written. The try block throws an exception which is caught later. Then Finally block is executed regardless of whether the exception is handled or not. Then a method called ClassB is defined. Then the finally block is executed regardless of whether the exception is handled or not. Then the main method is called.
Example #2
C# program to demonstrate the use of finally keyword in a program that has exception handling.
Code:
using System;
//a class called program is defined
public class program
{
// Main Method is called
static public void Main()
{
// two integer variables are defined to store two integers
intnum = 10;
int div = 0;
//try and catch block is defined in which an exception is raised by try block and is handled by catch block
try
{
int op = num / div;
}
catch (DivideByZeroException)
{
Console.WriteLine("The divisor can not be zero because it is impossible to divide by 0");
}
// finally block is executed regardless of the exception is handled or not
finally
{
Console.WriteLine("We are in the finally block");
}
}
}
Output:
Explanation: In the above program, a class called program is defined. Then the main Method is called. Then two integer variables are defined to store two integers. Then try and catch block is defined in which an exception is raised by try block and is handled by catch block. Then finally block is executed regardless of the exception is handled or not.
Example #3
C# program to demonstrate the use of finally keyword in a program that has no exception handling.
Code:
using System;
//a class called program is defined
public class program
{
// Main Method is called
static public void Main()
{
// two integer variables are defined to store two integers
intnum = 10;
int div = 0;
//try and catch block is defined in which an exception is raised by try block and is handled by catch block
try
{
int op = num / div;
}
// finally block is executed regardless of the exception is handled or not
finally
{
Console.WriteLine("We are in the finally block");
}
}
}
Output:
Explanation: In the above program, a class called program is defined. Then the main Method is called. Then two integer variables are defined to store two integers. Then try block is defined.Then finally block is executed regardless of the exception is handled or not. The output of the program is shown in the snapshot above.
Advantages
- Finally, the block of code is executed regardless of what happens within the try block like an exception is thrown or not thrown, if there is a return statement, nothing matters.
- The primary use of finally block of code is to release all the allocated expensive resources in the try block.
- Finally, block ensures that an operation will be performed regardless of any exceptions thrown.
Conclusion: In this tutorial, we understand the concept of finally keyword in C# through definition, syntax, working through programming examples, and their outputs.
Recommended Article
This is a guide to C# Finally. Here we discuss the Introduction to C# finally and its advantages along with its examples and Code Implementation. You can also go through our other suggested articles to learn more –