The
page_error event provides a way to capture page-level errors. For error handling, you can simply display an error message (as shown in the following example code), or you can log an event or perform some other action
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 purpose of the 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. Page_Error events, examples are 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. Example: Create a Web Program-new page pageevent.aspx, and then add the following code to pageevent.aspx: code as follows: <script language= "C #" runat= "Server" > void Page_Load (object sender, System.EventArgs e) {//throws a null reference exception keleyi.com throw (new ArgumentNullException ()); &N Bsp protected void Page_Error (object sender, EventArgs e) {Exception objerr = Server.GetLastError (). GetBaseException (); Get error string err = "1.error in: " + Request.Url.ToString () + "</br>" + "2.error message:   ; "+ objErr.Message.ToString () +" </br> "+" 3.stack Trace: "+ ObjErr.StackTrace.ToS TrinG () + "</br>"; Response.Write (Err. ToString ()); Output error message//Response.Redirect ("errorpage.htm"); Can redirect to friendly error page server.clearerror (); } </script> Save, right click on the page-"View in browser" to see the custom error message that was captured