Adding Code that Executes Before Exiting the Function


Part of the difficulty of handling exceptions is doing clean-up. Suppose you open a database and move a record from one table to another, but you receive an exception before you have a chance to close the database. If you leave the function without closing the database, your program will consume more memory and limit the number of connections from other users to the same database. C# provides a language construct that works in conjunction with the try/catch block that enables you to specify code that should run before you exit a function (whether you exit because of an exception or simply exit because the function is done executing). To add code that triggers automatically before a function exits, you must add a finally block.

To add a finally block:

  1. Put the code for the function inside a try block. Adding a catch section is optional.

  2. After the closing curly bracket for the try section, on the next line, type finally followed by a space and an open curly bracket { .

  3. Add clean-up codecode that you wish to execute before exiting the function.

  4. Add a close curly bracket } ( Figure 11.27 ).

    Figure 11.27 The finally block doesn't require you to have a catch block, only a try. It will always execute even if an exception doesn't occur.
     void OpenDatabase() {    string cstr =    "Provider=Microsoft.Jet.OLEDB.4.0;"    + @"Data Source=c:\csvqs.mdb;";    IDbConnection conn = new    OleDbConnection(cstr);    try    {       conn.Open();       OleDbCommand cmd = new       OleDbCommand(       "select * from authors",conn);       IOleDbReader reader =       cmd.ExecuteReader();    }    catch(OleDbException e)    {       string Msg = "Database Error:<br>";       Msg += e. Message;       Response.Write(Msg);    }  finally   {   conn.Close();   }  } 

graphics/tick.gif Tips

  • After try, you can add many catch handlers and one finally block. You have to put all catch blocks before the finally block.

  • Whether you catch an exception or not before leaving the function the .NET runtime will execute the code in the finally block.

  • It's not necessary to have a catch block to use finally.

  • You can use try/finally anywhere in your codethe try section doesn't need to contain all the code of the function ( Figure 11.28 ). The code in the finally block executes whenever you exit the try section of the try/finally block.

    Figure 11.28 In each iteration of the loop, whether there was an exception or not, the program will execute the code in the finally. Notice that if the code gets an exception that isn't being caught, the program will not only exit the loop, but the entire function as well, yet the finally will still execute.
     void FetchAuthors() {    string cstr =    "Provider=Microsoft.Jet.OLEDB.4.0;"    + @"Data Source=c:\csvqs.mdb;";    OleDbConnection conn = new    OleDbConnection(cstr);  using(conn)   {  conn.Open();       OleDbCommand cmd = new       OleDbCommand(       "select * from authors",conn);       IDataReader reader =       cmd.ExecuteReader();  }  } 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net