Danh mục

Using a finally Block

Số trang: 2      Loại file: pdf      Dung lượng: 17.12 KB      Lượt xem: 1      Lượt tải: 0    
Jamona

Phí lưu trữ: miễn phí Tải xuống file đầy đủ (2 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Sử dụng cuối cùng Block Điều quan trọng là hãy nhớ rằng khi một ngoại lệ được ném, nó thay đổi dòng chảy của thực hiện thông qua chương trình. Điều này có nghĩa bạn không thể đảm bảo rằng một tuyên bố sẽ luôn luôn chạy khi phát biểu trước kết thúc
Nội dung trích xuất từ tài liệu:
Using a finally Block Using a finally BlockIt is important to remember that when an exception is thrown, it changes the flow ofexecution through the program. This means you cant guarantee that a statement willalways run when the previous statement finishes, because the previous statement mightthrow an exception. Look at the following example. Its very easy to assume the call toreader.Close will always occur. After all, its right there in the code:TextReader reader = src.OpenText();string line;while ((line = reader.ReadLine()) != null){ source.Text += line + \n;}reader.Close();Sometimes its not an issue if one particular statement does not run, but on manyoccassions it can be a big problem. If the statement releases a resource that was acquiredin a previous statement, then failing to execute this statement results in the resource beingretained. This example is just such a case: If the call to src.OpenText succeeds, then itacquires a resource (a file handle) and you must ensure that you call reader.Close torelease the resource. If you dont, sooner or later youll run out of file handles and beunable to open more files (if you find file handles too trivial, think of databaseconnections instead).The way to ensure a statement is always run, whether or not an exception has beenthrown, is to write that statement inside a finally block. A finally block occursimmediately after a try block, or immediately after the last catch handler after a try block.As long as the program enters the try block associated with a finally block, the finallyblock will always be run, even if an exception occurs. If an exception is thrown andcaught locally, the exception handler executes first, followed by the finally block. If theexception is not caught locally (the common language runtime has to search through thelist of calling methods to find a handler), the finally block runs first. In any case, thefinally block always executes.The solution to the reader.Close problem is as follows:TextReader reader = null;try{ reader = src.OpenText(); string line; while ((line = reader.ReadLine()) != null) { source.Text += line + \n; }}finally{ if (reader != null) { reader.Close(); }}Even if an exception is thrown, the finally block ensures that the reader.Close statementalways executes. Youll see another way to solve this problem in Chapter 13, “UsingGarbage Collection and Resource Management.” • If you want to continue to the next chapter Keep Visual Studio 2005 running and turn to Chapter 7. • If you want to exit Visual Studio 2005 now On the File menu, click Exit. If you see a Save dialog box, click Yes.

Tài liệu được xem nhiều: