Partager via


Complete Example for Error Handlers

This code example includes elements for both page-level and application-level exception handling.

Code Example Files

The files in this code sample are as follows:

  • Web.config

  • Global.asax

  • Default.aspx

  • ExceptionUtility (to be put in the App_Code folder)

  • GenericErrorPage.aspx

  • HttpErrorPage.aspx

Web.config

The following example provides the Web.config file. The initial setting of customErrors will cause any unhandled errors to be directed to the HttpErrorPage.aspx file.

Global.asax

The following example provides the Global.asax file. To enable the error event handler in the Global.asax file, you will have to modify the Web.config file. The configuration file takes precedence. Therefore, you must either set customErrors to Off or remove the defaultRedirect setting. If your Web.config configuration file has customErrors set to Off, the Application_Error event handler in Global.asax will process all unhandled errors.

Security noteSecurity Note

Never set customErrors to Off in your Web.config file if you do not have an Application_Error handler in your Global.asax file. Potentially compromising information about your Web site can be exposed to anyone who can cause an error to occur on your site.

ExceptionUtility

The following example provides the ExceptionUtility file. Error logs might be directed to the computer's ErrorLog file, or, if the computer is part of a Web farm, the error log might be recorded in a globally available text file, or even a database. You might also need to immediately notify system administrators of a problem. The following ExceptionUtility has two static methods: one to log the exception, and one to notify system administrators. How those methods are implemented in your code depends on the needs of your organization. For this example, you must grant write permissions to the NETWORK SERVICE for the App_Data folder to enable the application to write to the error log.

Default.aspx

The following example provides the Default.aspx page. This file provides three buttons, each of which raises a different exception. The Page_Error handler on the page filters for specific errors, and handles these three exceptions in three separate ways. It also provides a link to a nonexistent file, which provides a fourth kind of error that is not handled on the page.

GenericErrorPage.aspx

The following example provides the GenericErrorPage.aspx page. This page creates a safe message that it displays to remote users. For local users (developers and testers of the application), the page displays a complete exception report.

HttpErrorPage.aspx

The following example provides the HttpErrorPage.aspx page. This page also creates a safe message that depends on the value of the error code, which it displays to remote users. For local users, the page displays a complete exception report.

See Also

Tasks

How to: Handle Page-Level Errors
How to: Handle Application-Level Errors

Other Resources

Error Handling in ASP.NET Pages and Applications