asp.net error handling application

Source: Internet
Author: User
Tags error handling visual studio

The

 application_error event is similar to the Page_Error event, and you can use him to catch errors that occur in the application. Because events occur throughout the application, you can log application error messages or handle other application-level errors that may occur

Asp. NET error handling Application_Error event examples are as follows:   New Web Program--New AppEvent.aspx page--Add the following code to the page:     code as follows: <script language=c# runat= "Server" > void Page_Load (object sender, System.EventArgs e) {throw (new ArgumentNullException ());} </SCRIPT>     Then, add the Application_Error event to the Global.asax file to catch the error that was raised in the Page_Load event of the AppEvent.aspx page. Add the following code to the Global.asax file: The   code is as follows: protected void Application_Error (object sender, EventArgs e) {Exception objerr = S Erver. GetLastError (). GetBaseException (); Get error keleyi.com String err = "Error caught in Application_Error event/n" + "error in:" + Request.Url.ToString () + "/nerro R message: "+ objErr.Message.ToString () +"/nstack Trace: "+ objErr.StackTrace.ToString (); Writes the caught error to the application log in Windows, where the application log can be accessed from the Event Viewer. System.Diagnostics.EventLog.WriteEntry ("Test2", err, System.Diagnostics.EventLogEntryType.Error); Server.ClearError (); Clears the exception, and the exception is no longer caught elsewhere.     Save the action you just made, in Visual Studio. NET, on the Build menu, click Build. Right-click the page, and then click View in the browser. In this case, the page will be blank, butIs that you should notice that a new item has been added to the event log. This example generates an entry in the Application log to access the application log from the Event Viewer. After logging an error, you may want to redirect the user to another user-friendly error page or perform some other action as needed, which can be done according to your needs.    
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.