21.3. Catching and E-Mailing Exceptions

In an e-commerce site, unhandled exceptions are more than an inconvenience to users and an embarrassment to site owners. Errors can be expensive because when the page that accepts credit card payments fails, the site loses sales. Sending an e-mail to the support desk (or whoever's minding the store) with a copy of the unhandled exception is useful. That way, someone can start investigating right away.

In ASP.NET, an unhandled exception triggers an event that you can catch in the global.asax file — a type of master code file. Follow these steps to create an e-mail message with the details of a site error:

  1. Add a Global Application Class file named Global.asax to your project (FileNew FileGlobal Application ClassAdd).

  2. In the Global.asax file, locate the Application_Error() subroutine.

    As indicated in the handler code comments, Application_Error() is the routine that runs when an unhandled exception occurs.

  3. Replace the existing Application_Error() subroutine with Listing 21-1.

  4. Change the e-mail addresses, passwords, and mail server details (shown in bold in Listing 21-1) with your Web site and mail server's information.

    Check with your ISP's technical ...

Get ASP.NET 3.5 For Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.