An exception is a mechanism for reporting errors. The error processing location may be far from the location where the error occurs. All information about the error cause must be stored in the exception object. In this process, we may want to translate low-level errors into specific application-related errors without losing any information related to source errors.
We need to understand when and why new exception classes should be created, and how to construct a valuable exception class hierarchy. When developers write catch statements, they implement different behaviors based on different types of exception objects at runtime. Each action needs to correspond to a different exception class.
Different runtime exception types have different catch statements. As the author of the class library, we must create or use different exception classes so that the catch statements can execute different behaviors, if this is not done, there will be few options available to users. Of course, we can directly suspend and exit the application when an application is thrown, but this is irresponsible.
Not all error conditions should throw exceptions. You can refer to the following principles: if they are not handled immediately or reported, it will lead to long-term problems, for example, data integrity errors in the database should cause exceptions. If such errors are ignored, the problem will roll larger and larger.
We adopt the principle when creating exception classes: people tend to over-use system when throwing exceptions. exception: This future call Code does not provide any useful information. On the contrary, after careful consideration, you can create necessary exception classes to let the caller understand the cause of the error, and provide it with the best processing method.
The following describes how to create an exception class. During the creation, we should not. exception class, instead of system. applicationexception is derived. applicationexception provides four constructors. We also need to create these four constructors and use the constructors in the base class.
The application always throws an exception. If we do not perform any specific processing. when a method in the net kernel framework is abnormal, the application will generate.. Net defined exception objects. providing more detailed information for exceptions can help first-level users diagnose errors and fix them as much as possible. Only when there are different fixes, we should create a variety of exception classes.