Original article: http://blog.joycode.com/dotey/archive/2005/01/20/43363.aspx
Custom exception classes in ASP. NET forums
Forums has a custom exception class forumexception, which inherits from applicationexception and corresponds to an enumeration forumexceptiontype to record the exception type. Forums custom exception classes are mainly used to handle various possible exceptions separately --Record error logs,Message prompt.
When an exception is thrown in Asp.net, the error event of httpapplication is triggered and can be captured in httpmodule. For example, forums is used to handle the forums exception in its httpmodule: application. error + = new eventhandler (this. application_onerror );. After a custom exception is caught, logs of important error messages are recorded (saved to the forums_exceptions table of the database) based on the exception type (forumexceptiontype enumeration) for the Administrator's reference. For exceptions, a friendly error message is displayed based on the exception class to give user-friendly prompts.
Forums custom exceptions not only serve to handle error exceptions, but also serve as information prompts. For example, if a user registration is successful, an exception is thrown: throw new forumexception (forumexceptiontype. useraccountcreatedauto); then, the system automatically jumps to the prompt that the user has successfully registered.
How does forums jump to the corresponding information based on exceptions? As mentioned above, forums has an error type enumeration for each custom exception. forums has a messages. XML for each language. The XML rules are as follows:
< Root > < Message ID = "1" > < Title > No permission to log on </ Title > < Body > You are not a super administrator. </ Body > </ Message > < Message ID = "2" > < Title > No permission to modify </ Title > < Body > This forum does not exist, or you are not authorized to modify it. </ Body > </ Message > </ Root >
The IDS correspond to the int type of forumexceptiontype one by one.
Public Enum Forumexceptiontype ... {Administrationaccessdenied= 1, Posteditaccessdenied= 2,}
When an exception is handled in the httpmodule, the response page will jump directly to the Message prompt page. redirect (globals. getsiteurls (). message (exception. exceptiontype), true); such as http: // localhost/cnforums/msgs/default. aspx? Messageid = 1 on the MSG display page, the corresponding prompt information is found from the XML according to the messageid, and displayed on the page.