
268
|
Chapter 12: The Page Class
This is the Title of the Book, eMatter Edition
Copyright © 2008 O’Reilly & Associates, Inc. All rights reserved.
Events Reference
Error
Sub Page_Error(Sender As Object, e As Event Args)
'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 excep-
tion 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 ...