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
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.
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ìm kiếm theo từ khóa liên quan:
ngôn ngữ lập trình lập trình ngôn ngữ C# C# Summarizing Keyword Combinations Using a finally BlockGợi ý tài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 261 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 261 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 230 0 0 -
Bài giảng Một số hướng nghiên cứu và ứng dụng - Lê Thanh Hương
13 trang 221 0 0 -
Giáo án Tin học lớp 11 (Trọn bộ cả năm)
125 trang 214 1 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 202 0 0 -
Bài tập lập trình Windows dùng C# - Bài thực hành
13 trang 177 0 0 -
Giáo trình Lập trình C căn bản: Phần 1
64 trang 169 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 162 0 0