Making Code Exception-Safe

Chia sẻ: Nghia Tuan | Ngày: | Loại File: PDF | Số trang:3

0
56
lượt xem
2
download

Making Code Exception-Safe

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Làm luật ngoại lệ-Safe Trong tập thể dục sau đây, bạn sẽ viết lại một mảnh nhỏ của code để làm cho nó exceptionsafe. Mã này sẽ mở ra một tập tin văn bản, đọc nội dung của nó một dòng tại một thời điểm, viết những dòng này vào một hộp văn bản phong phú về hình thức Windows, và sau đó đóng tập tin văn bản.

Chủ đề:
Lưu

Nội dung Text: Making Code Exception-Safe

  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;
  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.
  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 bộ tài khoản