Home All Groups Group Topic Archive Search About
Author
14 Apr 2005 8:40 PM
VK
Hello,

   I am trying to get the last occured error via the
following code:

Sub Page_Load(...)
Dim zero As Integer = 0
Try
  Dim err As Integer = 10 / zero
Catch ex As Exception
End Try

Response.write(Server.GetLastError.Message)
End Sub

However the above code always throws an error at the
response.write line with:

Object reference not set to an instance of an object.

Anybody knows why this is happening?

Author
14 Apr 2005 9:38 PM
Brad
Well, first off 10/0 will not throw an error.  I also assume you must have
Option Strict Off or your code would not even compile: err would need to be
dimmed as a double or you would need ctype(10/zero, double) for code to
compile.

I think GetLastError only returns the last unhandled error, during the
current context, and I think the page's Page_Error event or global.asax
Application_Error event has to be raised to access it.  Once one of those
events is raised Server.GetLastError is available anywhere in your
code...for the remainder of the current context / request

Example:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    Dim zero As Integer = 0
    Dim err As Double = 10 / CType("x", Integer)
End Sub

Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
    Response.Write(Server.GetLastError.Message)
End Sub

Or since it's now available anywhere, you could also do something like this:

Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
Handles MyBase.Error
    Server.Transfer("error.aspx")
End Sub

And then in error.aspx

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    Response.Write(Server.GetLastError.Message)
End Sub



Show quoteHide quote
"VK" <anonym***@discussions.microsoft.com> wrote in message
news:0dfb01c54132$353bf820$a401280a@phx.gbl...
> Hello,
>
>   I am trying to get the last occured error via the
> following code:
>
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
>  Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
>
> Response.write(Server.GetLastError.Message)
> End Sub
>
> However the above code always throws an error at the
> response.write line with:
>
> Object reference not set to an instance of an object.
>
> Anybody knows why this is happening?
Are all your drivers up to date? click for free checkup

Author
15 Apr 2005 2:01 PM
William F. Robertson, Jr.
Since you are catching the exception, it is no longer an error.

bill

Show quoteHide quote
"VK" <anonym***@discussions.microsoft.com> wrote in message
news:0dfb01c54132$353bf820$a401280a@phx.gbl...
> Hello,
>
>    I am trying to get the last occured error via the
> following code:
>
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
>   Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
>
> Response.write(Server.GetLastError.Message)
> End Sub
>
> However the above code always throws an error at the
> response.write line with:
>
> Object reference not set to an instance of an object.
>
> Anybody knows why this is happening?
Author
15 Apr 2005 2:15 PM
Matt Berther
Hello VK,

If this code would generate an error (which it wont as detailed by Brad),
you're swallowing it anyways. You need to remove the try/catch.

Show quoteHide quote
> Hello,
>
> I am trying to get the last occured error via the following code:
>
> Sub Page_Load(...)
> Dim zero As Integer = 0
> Try
> Dim err As Integer = 10 / zero
> Catch ex As Exception
> End Try
>
> Response.write(Server.GetLastError.Message)
> End Sub
> However the above code always throws an error at the response.write
> line with:
>
> Object reference not set to an instance of an object.
>
> Anybody knows why this is happening?
>

Bookmark and Share