Making Code Exception-Safe


Making Code Exception-Safe

In the following exercise, you will rewrite a small piece of code to make it exception-safe. The code opens a text file, reads its contents one line at a time, writes these lines to a rich text box on a Windows form, and then closes the text file. However, if an exception arises as the file is read or as the lines are written to the rich text box, the call to close the text file will be by-passed. You will rewrite the code to use a using statement instead, thus ensuring that the code is exception-safe.

Write a using statement

  1. Start Microsoft Visual Studio 2005.

  2. Open the UsingStatement project, located in the \Microsoft Press\Visual CSharp Step by Step\Chapter 13\UsingStatement folder in your My Documents folder.

  3. On the Debug menu, click Start Without Debugging.

    The Windows form appears.

  4. On the form, click Open File.

  5. In the Open dialog box, navigate to the \Microsoft Press\Visual CSharp Step by Step\Chapter 13\UsingStatement\UsingStatement folder in your My Documents folder and select the Form1.cs source file.

    This is the source file for the application itself.

  6. Click Open.

    The contents of the file are loaded into the Windows form.

    graphic

  7. Close the form to return to Visual Studio 2005.

  8. Open the Form1.cs source file in the Code and Text Editor window, and then locate the openFileDialog_FileOk method.

    This method should look like this:

    private void openFileDialog_FileOk(object sender,      System.ComponentModel.CancelEventArgs e) {     string fullPathname = openFileDialog.FileName;     FileInfo src = new FileInfo(fullPathname);     filename.Text = src.Name;     source.Text = "";     TextReader reader = new StreamReader(fullPathname);     string line;     while ((line = reader.ReadLine()) != null)     {         source.Text += line + "\n";     }     reader.Close(); }

    The variables filename, openFileDialog, and source are three private fields of the Form1 class. The problem with this code is that the call to reader.Close is not guaranteed to happen. If an exception occurs after opening the file, the method will terminate with an exception, but the file will remain open until the application finishes.

  9. Rewrite the openFileDialog_FileOk method with a using statement, exactly as follows:

    private void openFileDialog_FileOk(object sender,      System.ComponentModel.CancelEventArgs e) {     string fullPathname = openFileDialog.FileName;     FileInfo src = new FileInfo(fullPathname);     filename.Text = src.Name;     source.Text = "";     using (TextReader reader = new StreamReader(fullPathname))     {         string line;         while ((line = reader.ReadLine()) != null)         {             source.Text += line + "\n";         }     } }

    Notice that you no longer need to call reader.Close as it will be invoked by the Dispose method of the StreamReader class when the using statement finishes. This applies whether the using statement finishes naturally or terminates because of an exception.

  10. Rebuild and re-run the application to verify that it still works.

  • If you want to continue to the next chapter

    Keep Visual Studio 2005 running and turn to Chapter 14.

  • If you want to exit Visual Studio 2005 now

    On the File menu, click Exit. If you see a Save dialog box, click Yes.




Microsoft Visual C# 2005 Step by Step
Microsoft® Visual C#® 2005 Step by Step (Step By Step (Microsoft))
ISBN: B002CKYPPM
EAN: N/A
Year: 2005
Pages: 183
Authors: John Sharp

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