tailieunhanh - Apress - Accelerated C#_3
Tham khảo tài liệu 'apress - accelerated c#_3', công nghệ thông tin, cơ sở dữ liệu 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 public EmployeeVerificationException Cause reason String msg Exception inner base msg inner reason protected EmployeeVerificationException SerializationInfo info StreamingContext context base info context public Cause Reason get private set In the method you can see the simple call to Validate on the emp object. This is a rather crude example where you force the validation to fail by throwing an EmployeeVerificationException. But the main focus of the example is the creation of the new exception type. Many times you ll find that just creating a new exception type is good enough to convey the extra information you need to convey. In this case I wanted to illustrate an example where the exception type carries more information about the validation failure so I created a Reason property whose backing field must be initialized in the constructor. Also notice that EmployeeVerificationException derives from . At one point the school of thought was that all .NET Framework-defined exception types would derive from while all user-defined exceptions would derive from ApplicationException thus making it easier to tell the two apart. This goal has been lost partly due to the fact that some .NET Framework-defined exception types derive from ApplicationException. 7 You may be wondering why I defined four exception constructors for this simple exception type. The traditional idiom when defining new exception types is to define the same four public constructors that exposes. Had I decided not to carry the extra reason data then the EmployeeVerificationException constructors would have matched the constructors exactly in their form. If you follow this idiom when defining your own exception types users will be able to treat your new exception type in the same way as other system-defined exceptions. Plus your derived exception will be .
đang nạp các trang xem trước