Global.cs Exception _ Practical tips for automatically getting unhandled exceptions in the

Source: Internet
Author: User
Here is a simple example:
1 code that causes the exception
Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{
int a = 5;
int b = 2;
int C;
c = A/(b-2);
}

2) code in the Global.cs
Copy Code code as follows:

protected void Application_Error (object sender, EventArgs e)
{
Get exception information
String strerror = Server.GetLastError (). ToString ();
You can write error messages through log4net to text or libraries, which are written only on the page
Response.Write ("error message:" + strerror + "");
Server.ClearError ();
}

Through the above practice I wrote a demo in the F5 debugging sometimes will output error messages on the page sometimes first in the abnormal place to stop running but then press the F5 error message appears. In IE, FF, chrome are the same problem, it is possible that the browser caching problem. After the launch of their own demo after the view of all normal.

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.