ASP.net provides three primary methods for capturing and responding to errors when errors occur: Page_Error events, Application_Error events, and application configuration files (Web.config).
The main effect of these three methods is to create custom error reports, and to create custom error reports: For security reasons, there is also the intention to show friendly error pages to the user and improve the program's friendliness.
The Page_Error incident is illustrated by examples as follows:
This example displays detailed error information in the browser, which is provided for illustration purposes only. It is very dangerous to show the end users of an application the details of the error (especially web programs). It is more appropriate to display a message to the user informing them that an error has occurred and then logging the specific error message in the event log.
This example tests the Page_Error event by intentionally throwing an empty exception in the Page_Load event.
For example: Create a Web Program-new page pageevent.aspx, and then add the following code to pageevent.aspx:
Copy Code code as follows:
<script language= "C #" runat= "Server" >
void Page_Load (object sender, System.EventArgs e)
{
Throw a null reference exception keleyi.com
Throw (new ArgumentNullException ());
}
protected void Page_Error (object sender, EventArgs e)
{
Exception objerr = Server.GetLastError (). GetBaseException (); Get errors
string err = "1.error in:" + Request.Url.ToString () + "</br>" +
"2.error message:" + objErr.Message.ToString () + "</br>" +
"3.stack Trace:" + objErr.StackTrace.ToString () + "</br>";
Response.Write (Err. ToString ()); Output error message
Response.Redirect ("errorpage.htm"); Can redirect to a friendly error page
Server.ClearError ();
}
</script>
Save, right click on the page-"View in browser" to see the custom error message caught