Professional Microsoft Smartphone Programming phần 7

Bởi vì truy cập vào một cơ sở dữ liệu là bình thường dễ gây lỗi, nó luôn luôn là một thực hành tốt để đưa các hoạt động databaserelated trong cố gắng ngăn chặn và xử lý các trường hợp ngoại lệ có thể có trong khối catch. Hình 13-5 và 13-6 cho thấy những kết quả hoạt động của ứng dụng. | Chapter 11 When throwing a user-defined exception you have the option to use one of the available constructors. In the preceding example code you can use either the default constructor or the one that accepts a string parameter User-defined exception Invalid Zip Code or the one that accepts the string parameter and an exception instance new ArgumentException . For example the following two throw calls can also be used in the preceding example throw new InvalidZipCodeException throw new InvalidZipCodeException User-defined exception Invalid Zip Code. Any code that calls ProcessZipCodeInput should be put into a try-catch block. For example if a string 239d3 is passed to the ProcessZipCodeInput method an exception will be thrown and should be handled try ProcessZipCodeInput 239d3 catch InvalidZipCodeException e Best Practices of Exception Handling As shown in the sample code discussed so far exception handling can act as a method for execution event notification across method function calls. Note the following issues regarding when where and how to use exception handling to improve the robustness of an application Exceptions vs. condition checking. Some errors can be discovered by checking whether the variable truly holds the expected value or by checking the return value of a method. Without using exceptions the code can simply return some error code in the case of a specific type of error. The key is to determine whether this kind of error occurs quite rarely in the code path. On the one hand if the error is indeed exceptional using exceptions is recommended because you won t waste CPU cycles on that condition-checking code in normal cases. On the other hand if the error is almost certain to occur every time in the current method you may prefer condition checking to exception handling because the latter usually results in significant overhead of stack unwinding and exception creation and deletion. Exceptions vs. return error code. A .

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
10    482    3    16-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.