accelerated c# 2010 trey nash phần 5

Tham khảo tài liệu 'accelerated c# 2010 trey nash phần 5', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | CHAPTER 7 EXCEPTION HANDLING AND SECEPTION SAFETY else false Failed to call Dispose on RollbackHelper private Database db private bool disposed false private bool committed false public class EntryPoint static private void DoSomeWork using RollbackHelper guard new RollbackHelper db Here we do some work that could throw an exception. Comment out the following line to cause an exception. If we get here we commit. static void Main db new Database DoSomeWork static private Database db static private Object nullPtr null Inside the DoSomeWork method is where you ll do some work that could fail with an exception. Should an exception occur you ll want any changes that have gone into the Database object to be reverted. Inside the using block you ve created a new RollbackHelper object that contains a reference to the Database object. If control flow gets to the point of calling Commit on the guard reference all is well assuming the Commit method does not throw. Even if it does throw you should code it in such a way that the Database remains in a valid state. However if your code inside the guarded block throws an exception the Dispose method in the RollbackHelper will diligently roll back your database. No matter what happens the Dispose method will be called on the RollbackHelper instance thanks to the using block. If you forget the using block the finalizer for the RollbackHelper will not be able to do anything for you because finalization of objects goes in random order and the Database referenced by the RollbackHelper could be finalized prior to the RollbackHelper instance. To help you find the places where you brain-froze you can code an assertion into the helper object as I have previously done. The whole use of this pattern hinges on the using block so for the sake of the remaining discussion let s assume you didn t forget it. Once execution is safely inside the Dispose method and it got there via a call to Dispose rather than

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
7    732    3    30-06-2024
96    121    5    30-06-2024
25    92    2    30-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.