Customize a unified set of exception handling mechanisms for your site

Source: Internet
Author: User
Tags exception handling net thread

People who have written the program know that any good program can be a failure to deal with the exception, because the program running environment and the way people operate can be said to be different, developers in the beginning it is difficult to think of all the situation, and do the corresponding processing. As a result, developers need to work together with testers in order to minimize and eliminate errors in programs that are, of course, just ideal, and to handle as many exceptions as possible. In a variety of applications, the challenges of web sites can be said to be the larger of the various programs. Why do you say that? The reason is very simple, a website users vary widely, user habits are different, users use the computer and software platform is also different, the network environment is far apart, so the probability of the site abnormal situation is also relatively large. Since the anomaly is unavoidable, we should develop a set of exception handling mechanism for our website, that is, how to make it more reasonable for the abnormal aftermath that has occurred. The discussion in this article is limited to. NET environment in the development of the Web site exception handling, the other platform of the truth should be the same, but the specific way to achieve different.

In fact, most of the anomalies are divided into two categories, the first category is you know that may occur, that is, developers in the process of writing the program already know that there may be some unexpected situation. The exception to this situation can be divided into the following situations: First, the occurrence of the exception will not affect the normal execution of the program, just to get the exception correctly captured, and do the corresponding processing can; Secondly, when the exception occurs, the program can not complete the normal processing logic, this time need to jump exception handling logic up, Prompts or notifies the administrator or end user what is happening. So that the administrator or user to do the appropriate processing before the normal execution of the program logic. For example, users want to query the database of a data record, when the program tries to connect to the target database when the database server down machine, normal query operation has no way to continue. This requires the program in processing such as the connection database operation, the first to determine whether the database is running normally, once the database is caught to run an exception, you should immediately log the error log and report to the database administrator, while giving user friendly hints; Third, some exceptions are the need for normal processing logic. Like what. NET Thread.Abort () will raise a ThreadAbortException, Invoke Response.End (), Response.Redirect (), Response.transfer () Also triggers a ThreadAbortException, which is used to control the normal logic. This kind of exception generally does not require you to do too much extra processing. It's important to note that when we write our program, we try not to treat the exception as part of the normal processing logic, which can lead to inefficient program execution. As an example:

int temp = 0;

try
{
temp = Int32.Parse(input);
}
catch
{
temp = 1;  // 1是默认值
}

The above code treats exception handling as part of the normal processing logic, which makes the execution of the program inefficient. The above code is better written in the following form:

int temp = 0;

if(Int32.TryParse(input, out temp) == false)
{
temp = 1; // 1是默认值
}

Now, then, to remove the first class exception, there is an exception that is the second type of exception is the program developers in the beginning did not think of some of the exception, has been beyond the scope of the process writers. This type of anomaly is deadly and will have a significant impact on the system once it happens. Because by this time, the program has been running out of control of our scope.

For the first class of exceptions, programmers have basically solved them in place. For the second type of exception, it is necessary to establish a unified processing mechanism. For a medium-scale web site, the business logic is more complex and involves a lot of web pages. We do not know in advance which page or which program will be abnormal (at least in the development of the program did not fully think of some of the exceptions), it is necessary to have such a unified set of exception handling mechanism. There are two possible ways to do this, the first way is to create a base class for all pages of the site, the base class inherits from the System.Web.UI.Page, and the various exceptions are uniformly handled in the base class. The implementation is to add an exception handling event for the base class, that is, this. Error + = new System.EventHandler (this. PAGEBASE_ERROR); Implement your exception handling logic in the method Pagebase_error.

protected void PageBase_Error(object sender, System.EventArgs e)
{

             Exception currentError = Server.GetLastError();

             if (currentError == null)
             {
                 return;
             }

             LogError(currentError);  // 把错误信息写到你的日志列表里面,也可以同时给系统管理员发送一封邮件。

             string friendlyMsg = "你的友好错误提示";

#if DEBUG  // 如果程序处于Debug状态,则直接在页面显示具体错误信息,便于调试

             friendlyMsg = currentError.Message + “<br />Stack: ” + currentError. StackTrace;

#endif

             // 下面的信息是显示给用户的友好提示信息

             Response.Write(friendlyMsg);  // 你也可以重定向到某个错误提示页面。这里要小心一点的是,确保你的错误提示页面绝对不要有个Bug,否则可能会形成死循环。所以,错误提示页面最好就是一张完全静态的HTML页面。

             Server.ClearError();

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.