C# Finally Keyword4 Sept 2025 | 6 min read In the C# programming language, the finally block is a key feature of exception handling. It is used to write clean-up code that will always execute, whether an exception occurs or not. It is useful for several tasks, such as closing files, releasing the database, or disconnecting from a database. A finally block is written at the end, after the try/catch block. The finally statement code always runs in the program, which makes it a reliable place for resource management. Syntax of Finally keyword:It has the following syntax. In this syntax,
Simple finally block Example in C#Let's take an example to illustrate the finally block in C# exception handling. ExampleCompile and RunOutput: It has the following output: Case 1: (Valid Input) If we enter a positive number, the output would be: Enter any positive number: 5 The result is 2 Finally block has been executed. The program continues execution after exception handling. Case 2: (Invalid Input) If we enter the alphabetic value, the output would be: Enter any number for your choice: abc Please enter any valid number. Finally block has been executed. The program continues execution after exception handling. Case 3: (Divide by Zero) If we enter the 0 number, the output would be: Enter any number for your choice: 0 Division by zero is not allowed. Finally block has been executed. The program continues execution after exception handling. Explanation: In this example, we demonstrate how the finally block works in C# exception handling. First, we have taken the try block where the user is prompted to enter a number to perform the division operation. If the user enters a valid input, the program runs and shows the output. If the user enters invalid input, such as zero or an alphabet, an exception occurs. However, the finally block always executes, whether the user input is valid or not. Characteristics of Finally Blocks in C#Several characteristics of the finally block in C# are as follows:
Execution of the finally block without a catch blockIn the C# programming language, the finally block can be used without a catch block, and it always executes after the try block. These features are very helpful if we want to run cleanup code without handling the exceptions directly. C# finally block Example without a catch block:Let us take an example to use the finally block without using a catch block in C#. ExampleCompile and RunOutput: Welcome to Tpointtech The division of two numbers is: 40 Finally block has been executed successfully. The program continues after the try finally. Explanation: In this example, we illustrate the use of finally blocks without a catch block. Inside the try block, we print a message and perform a division operation. After that, the final block executes automatically and prints the confirmation message. Execution of the finally block with an exceptionIn C# programming, the execution of the finally block with an exception means that if an error happens in the try block, the code inside the finally block will still run. It ensures that tasks like clean up or resource release are always completed. C# Example using a finally block with exceptionsLet us take an example to illustrate the finally block with exceptions in C#. ExampleCompile and RunOutput: Error: Attempted to divide by zero. Finally block executed. Explanation: In this example, we try to divide the number 10 by 0 inside a try block. The divide by zero operation is invalid, so it causes a runtime error. After that, the program moves to the catch block. It finds the error and displays the error message. After the catch block, the finally block executes. C# Example using a finally block for resource clean-upLet us take an example to illustrate the finally block for clean-up resources. ExampleCompile and RunOutput: File not found: Could not find file "/home/compiler/ test.txt " Explanation: In this example, the program attempts to open or read the file. If the file is not found, the catch block handles the FileNotFoundException and displays an error message. At last, the finally block executed. The finally block checks if the r object is not null and then closes it to release system resources. Important Points in the finally block in C#Several important points of the finally block in C# are as follows:
ConclusionIn conclusion, the C# finally block is an important factor in exception handling by handling the run-time errors and maintaining the normal flow of the program. The finally block is the key feature of exception handling. It ensures that the clean-up code will always execute, whether an exception occurs or not. It is useful for tasks like closing files, releasing the database, or disconnecting from a database. C# finally Block FAQs1) What is a finally block in C#? In C#, the finally block is a key feature of exception handling. It ensures that the clean-up code will always run, whether an exception occurs or not. It is useful for tasks like closing files, releasing the database, or disconnecting from a database. A finally block is written at the end, after the try and catch block. 2) Is a finally block compulsory when handling an exception? No, the finally block is an optional block in exceptional handling. It is useful for tasks like closing files, releasing the database, or disconnecting from a database. 3) When does the finally block execute? The finally block runs in all the cases. It always runs whether exceptions occur or not. The finally block executes after the try and catch block completes. 4) Does C# allow a finally block without a catch block? Yes, the finally block can be utilized without using a catch block. 5) How are exceptions handled when they occur in the finally block? If an exception occurs in the finally block, it will suppress any exception thrown in the try or catch block. Next TopicC# User-Defined Exceptions |
We request you to subscribe our newsletter for upcoming updates.