asp.net error Capture page

Source: Internet
Author: User
Tags error handling stack trace

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: &nbsp ;     "+ 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  
Related Article

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.