Name
GetLastError
Synopsis
LastException = Server.GetLastError( )
Returns the last exception thrown.
Parameters
-
LastException
An object of type Exception to receive the application’s last exception.
Example
The code in GetLastError.aspx
displays a button
that, when clicked, calls a server-side event handler that declares
three Integers and purposely divides by zero (a no-no) to cause an
exception. The code in the Page_Error event handler declares an
Exception object and a String, gets the Exception using GetLastError,
tests whether the object reference is valid and assigns the Message
property of the exception to the string variable, and then writes the
value to the browser using the Response.Write method. You can also
call the GetLastError method from the Application_Error event
handler, which you can place in the global.asax
file to catch errors that have not been handled at the page level.
<%@ Page Language="VB" %> <html> <head> <title>Examining the Last Error</title> <script runat="server"> Sub CauseError(sender As Object, e As EventArgs) Dim x, y, z As Integer y = 1 z = 0 x = y / z End Sub Sub Page_Error(Source As Object, E As EventArgs) Dim LastError As Exception Dim ErrMessage As String LastError = Server.GetLastError( ) If Not LastError Is Nothing Then ErrMessage = LastError.Message Else ErrMessage = "No Errors" End If Response.Write("Last Error = " & ErrMessage & "<br/><br/>") Server.ClearError( ) End Sub </script> </head> <body> <form runat="server"> <h4><font face="verdana">Cause ...
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.