Name

Error

Synopsis

Sub Page_Error(Sender As Object, e As EventArgs)
   ‘error handling code
End Sub

The Error event is fired when an unhandled exception occurs on the page. If no event handler is defined for this event, the Application_Error event is fired. If the exception is still not handled, control is passed to the page (or pages) defined in the <customErrors> element in web.config.

Parameters

Sender

An argument containing information about the object that raised the event.

e

An object of type EventArgs containing additional information about the event.

Example

The following code example deliberately causes an overflow exception and then handles that exception in the Page_Error handler, displaying the text of the exception and then clearing it:

Sub Page_Load(  )
   Dim x, y, overflow As Integer
   x = 1
   y = 0
   overflow = x / y
End Sub
  
Sub Page_Error(  )
   Response.Write(Server.GetLastError.ToString(  ))
   Server.ClearError
End Sub

Notes

The current exception is obtained using the GetLastError method of the Server class. Once you’ve finished with your error handling, you can either clear the exception by calling Server.ClearError, as shown in the example, or allow the exception to bubble up to the next level of error handling.

Note that the Sender and e arguments are optional for this event, as shown in the example.

When the AutoEventWireup attribute of the @ Page directive is set to True (the default), ASP.NET will automatically call the event handler for this event, as long as it has the correct ...

Get ASP.NET in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.