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 }
Below are the examples of C# Finally:
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.
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.
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.
Conclusion: In this tutorial, we understand the concept of finally keyword in C# through definition, syntax, working through programming examples, and their outputs.
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 –
The above is the detailed content of C# finally. For more information, please follow other related articles on the PHP Chinese website!