Global.cs automatically gets unhandled exceptions

  • 2020-05-16 06:44:46
  • OfStack

Here's a simple example:
1) the code that caused the exception
 
protected void Button1_Click(object sender, EventArgs e) 
{ 
int a = 5; 
int b = 2; 
int c; 
c = a / (b - 2); 
} 

2) the code in Global.cs
 
protected void Application_Error(object sender, EventArgs e) 
{ 
// Get exception information  
string strError = Server.GetLastError().ToString(); 
// You can pass the error message log4net Write to the text or library, only on the page  
Response.Write(" The error message :" + strError + ""); 
Server.ClearError(); 
} 

I wrote an Demo using the above method. When debugging F5, sometimes an error message will be printed on the page, and sometimes it will stop running in the abnormal place, but then I will press F5 and the error message will appear. In the case of IE, FF and Chrome, the problem may be browser caching. Then I will post Demo and browse it again.

Related articles: