The try..catch..finally block in .NET allows developers to handle runtime exceptions. The syntax has three variations, try..catch, try..finally, and try..catch..finally. Learn more here: Exception Handling in C#
The code example shows a try catch finally block syntax.
- try
- {
- //Put suspected code here. When an exception occurs, the control will move to the catch block
- }
- catch
- {
- //Catch the exception
- }
- finally
- {
- //this block of code will always get executed whether an
- // exception occurs or not as long as there are no exceptions
- // within this block itself. If an Exception is thrown here
- // some outer try block should handle it
- //Any Clean up code goes here. Especially to release any
- //system resources such as file handles and network
- //connections
- }
Let's go through an example so that we can better understand the purpose of the finally block. The following code illustrates the use of the finally block to clean up file states.
- namespace TestFinally
- {
- using System;
- using System.IO;
- /// <summary>
- /// Summary description for TestFinally.
- /// </summary>
- public class TestFinally
- {
- public TestFinally()
- {
- // TODO: Add Constructor Logic here
- }
- public static void Main()
- {
- StreamReader sr1 = null;
- StreamWriter sw1 = null;
- try
- {
- System.Console.WriteLine("In try block");
- //Open files
- sr1 = new StreamReader(File.Open("Test1.txt",
- System.IO.FileMode.Open));
- sw1 = new StreamWriter(File.Open("Test2.txt",
- System.IO.FileMode.Append,
- FileAccess.Write));
- while (sr1.Peek() != -1)
- {
- sw1.WriteLine(sr1.ReadLine());
- }
- goto MyLabel;
- }
- catch (Exception e)
- {
- Console.WriteLine(String.Concat(e.Message,
- e.StackTrace));
- }
- finally
- {
- //Int32 j ;
- //j=0;
- //j=j/5;
- //If an Exception is thrown here some outer
- //try block should handle it. The finally block
- // will not be completely executed
- Console.WriteLine("In finally block");
- //we need to make sure that Close method of the
- //Streamreader sr1 or StreamWriter sw1
- //is called irrespective of whether an exception
- // occurs or not
- //to release any system resources associated with
- //the reader or writer
- if (sr1 != null)
- {
- sr1.Close();
- }
- if (sw1 != null)
- {
- sw1.Close();
- }
- }
- MyLabel:
- Console.WriteLine("In Mylabel");
- // note: the following statements are not allowed
- //inside a finally block:
- //return, goto, break and continue
- Console.ReadLine();
- }
- }
- }
In the above code, inside the Main method, we opened two files. First we opened the Test1.txt using a StreamReader object by passing a FileStream object returned by the File.Open method, and then we opened Test2.txt through a StreamWriter object by passing a FileStream object returned by the File.Open method. The intention here is to copy lines of Test1.txt and append them to Test2.txt. If the code in the try block executes successfully, we want to close StreamReader sr1 and StreamWriter sw1 so that system resources associated with the reader/writer are released.
If Test1.txt opens successfully but for some reason there is an exception thrown while opening Test2.txt, we still want to close the stream associated with Test1.txt. So in this example we have added the close methods in the finally code block so that they can be positively executed. We are checking for a null value because if none of the files is opened, we would get a nulldereferencing exception in the finally block.
Conclusion
Hope this article helped you understand how to use the try..catch..finally block in C#.
Learn more here: Exception Handling in C#
A detailed article on C# Try Catch

LiyaPosted Jun 3, 2010, 6:36 AM
This article is much more contrbuting . Thank you