"Go" ASP. NET captures exception errors in the entire solution using Global.asax

Source: Internet
Author: User

Before doing the project is in each page to deal with the different exception information, a page count down, a lot of try{}catch{} statement block, the entire code structure is not beautiful.

See a post today about using the global application class to help get exception information, using the server. Transfer ("") specifies the page to accept the error, plus the use of server in the Accept error page. GetLastError () Gets the previous exception source.

The Application_Error function in Global.asax is as follows:

       protected void Application_Error (object sender, EventArgs e)         {             //captures all exceptions under the entire solution             try             {                 Server.Transfer ("~/error.aspx");             }             Catch {}         

Error Accept page error.aspx the relevant code for getting exception information is as follows:

Exception ex = Server.GetLastError (). GetBaseException (); Gets the exception source                 if (ex! = null)                 {                       Response.Write (ex. Message);                     }                 Clears the previous exception                 

The test exception code in test page text.aspx is as follows:

Tests whether the exception information was captured     //test1    //int userid = Convert.ToInt32 (request["userid"). ToString ());        Test2    String Name = "Aganar";      

Run the Test.aspx page, we will see the relevant exception information, we can clearly see that there is no one try{}catch{} statement block exists in the page test.aspx page, we can easily capture the exception information.

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.