SIGN UP MEMBER LOGIN:    
ARTICLE

Try-Finally in C#

Posted by Puran Mehra Articles | C# Language December 20, 2009
In this article I will explain you about Try-Finally in C#.
Reader Level:

This article has been excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors of C# Corner.

If you are interested in executing a certain block of code irrespective of whether an exception occurs and you do not worry about catching an exception, then you'll likely want to use the try-finally statement. However, we do not recommend using this statement because you will not be able to see what the exception is about. Instead, you should use the try-catch-finally statement. Listing 7.8 shows the structure of the Try-Finally block:

Listing 7.8: Try-Finally Syntax

            try
            {
                //Even if a goto statement were here
                //control gets transferred to the statement identified in
                //goto statement only after executing the finally block.
            }

            finally
            {
                // This block of code will always get executed
                // whether an exception occurs or not
                // Any cleanup code goes here,
                // especially to release any system resources
                // such as file handles and network connections
            }


First the try block is executed, and then the finally block is executed, irrespective of whether an exception occurs or not. Even if a goto statement is present in the try block, the control gets transferred to the label in the goto statement only after executing the finally block.

Conclusion


Hope this article would have helped you in understanding Try-Finally in C#. See other articles on the website on .NET and C#.

visual C-sharp.jpg The Complete Visual C# Programmer's Guide covers most of the major components that make up C# and the .net environment. The book is geared toward the intermediate programmer, but contains enough material to satisfy the advanced developer.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor