1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Making Code Exception-Safe docx

3 235 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Making code exception-safe
Thể loại exercise
Định dạng
Số trang 3
Dung lượng 18,21 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 arise

Trang 1

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

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;

Trang 2

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:

10 private void openFileDialog_FileOk(object sender,

11 System.ComponentModel.CancelEventArgs e)

12 {

13 string fullPathname = openFileDialog.FileName;

14 FileInfo src = new FileInfo(fullPathname);

15 filename.Text = src.Name;

16 source.Text = "";

17 using (TextReader reader = new StreamReader(fullPathname))

18 {

19 string line;

20 while ((line = reader.ReadLine()) != null)

21 {

22 source.Text += line + "\n";

23 }

24 }

}

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

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

Trang 3

• 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

Ngày đăng: 26/01/2014, 13:20

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm