JavaScript Editor jscript editor     Web designer 

Main Page

This code example shows a handler for the Error event in an ASP.NET Web page. The handler catches all unhandled exceptions on the current page — in other words, all of the errors that are not caught with a Try/Catch block.


Visual BasicВ CopyCode imageCopy Code
Private Sub Page_Error(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Error
    Response.Write("An application error has been logged.")
End Sub
private void Page_Error(object sender, EventArgs e)
    Response.Write("An application error has been logged.");

Compiling the Code

This example requires:

  • An ASP.NET Web page.

  • An error-logging function that is specific to your application.

Robust Programming

It is preferable to use Try/Catch blocks around any code that is subject to errors rather than rely on a page-level error handler.

You cannot display error information in a control (such as a Label control) because new instances of controls are not created on the page when the Error handler is called.

After handling an error, you must clear it by calling the ClearError method of the Server object (HttpServerUtility class).


Use the <customErrors> Element to restrict display of detailed error messages to local users only.

Be sure that you do not display error information that might help malicious users compromise your application. For details, see How to: Display Safe Error Messages.

See Also

JavaScript Editor jscript editor     Web designer