---
title: "Use Finally Blocks in C#"  
description: "In this blog I am trying to explain the concept of Finally Blocks in C#Finally BlockA finally block is always executed, regardless of whether an excep"  
author: "Vijay Shukla"  
published: 2013-06-17  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/537/use-finally-blocks-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Use Finally Blocks in C#

In this blog I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to [explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of Finally Blocks in C#

[Finally Block](https://www.mindstick.com/articles/12106/exception-handling-in-java-guidelines-on-the-use-of-finally-block)

A finally block is always executed, regardless of whether an exception is thrown. Finally provides a construct for ensuring the correct [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of programs. It ensures a block of statements are always reached before the enclosing method is exited.

##### Working of Finally Block

When an [exception occurs](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs), execution stops and control is given to the closest exception handler. This often means that lines of code you expect to always be called are not executed. Some [resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) cleanup, such as closing a file, must always be executed even if an exception is thrown. To accomplish this, you can use a finally block.

Example: -

```
  using System;  class MindsctickArgumentOutOfRangeExample  {  public static void Main()  {  int[] arrayOne = { 0, 0 };  int[] arrayTwo = { 0, 0 };  try  { Array.Copy(arrayOne, arrayTwo, -1); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine("Error Occured:: "+ ex); } finally { Console.WriteLine("\n\nThis statement is always executed. Because its written in finally block.");
 } Console.ReadKey(); } }
```

##### Code Description: -

1. Using System (include the System [namespace](https://www.mindstick.com/articles/12552/namespace)).\
2. Create MindsctickArgumentOutOfRangeExample class.\
4. Create Main method.\
6. Create int[] type array with arrayOne named\
7. Create int[] type array with arrayTwo named\
8. Start try block.\
10. In this line trying to [copy array](https://www.mindstick.com/forum/156576/copy-array-items-into-another-array-via-javascript) source array to [destination](https://yourviews.mindstick.com/story/1451/gorgeous-locations-for-destination-weddings-in-india) array.\
12. Start catch block.\
14. Statement for catch block.\
16. Start finally block.\
18. Statement for finally block.

##### Output: -

![Use Finally Blocks in C#](https://www.mindstick.com/blogs/b62c5230-5747-4cf0-9fa8-36bae6459032/images/a5650a46-9227-40e3-968f-955161f2597b.png)

---

Original Source: https://www.mindstick.com/blog/537/use-finally-blocks-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
