473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Server.GetLastError

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?
Nov 19 '05 #1
4 4097
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

"VK" <an*******@discussions.microsoft.com> wrote in message
news:0d****************************@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?

Nov 19 '05 #2
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

"VK" <an*******@discussions.microsoft.com> wrote in message
news:0d****************************@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?

Nov 19 '05 #3
Since you are catching the exception, it is no longer an error.

bill

"VK" <an*******@discussions.microsoft.com> wrote in message
news:0d****************************@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?

Nov 19 '05 #4
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.

--
Matt Berther
http://www.mattberther.com
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?

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ASP.Confused | last post by:
I have a program that I'm trying to get error detection to work with. To test it, I attempted using the following mispelled CreateObject statement (which should cause an error): Set myObject =...
3
by: ad | last post by:
I use GenericErrorPage.aspx for the redirect page when some error occur. I want to show some different according error statusCode . How can I get error statusCode form Server.GetLastError()?
2
by: Lars Netzel | last post by:
I'm trying to make my own custom error page... but when using Try lblErrormessage.Text = Server.GetLastError.Message lblErrormessage.Text += "<br>" & Server.GetLastError.StackTrace ...
6
by: José Joye | last post by:
Hello, I'm currently reading the MS Developing Web applications with c# (and VB.net). In the chapter related to Error management, there is a sample about "Page-Level Error Pages" eg: In my...
3
by: ADavidson | last post by:
I'm getting a {"Parser Error: The Runat attribute must have the value Server." } error when I try to get the Server.GetlastError() in the Global.asax codebehind. Why am I getting this? I...
7
by: jtfaulk | last post by:
I need to encode some information on the server side using ASP.NET with C#; sending via HTTP to a client side application, that needs to be decoded in an MFC C++ application. I'm not sure if I...
7
by: dhnriverside | last post by:
Hi peeps I'm just following this HOW-TO from MSDN.. http://support.microsoft.com/default.aspx?scid=kb;en-us;306355 But I've got a problem. I've adding the #using System.Diagnostics; line to...
2
by: Richard Coltrane | last post by:
Hi there, Ive just implemented some application level exception handling in ASP.Net 2.0. I deliberately set up a null reference error in my code to see how this would be handled. Sure enough...
2
by: Seguros Catatumbo | last post by:
Hello guys, i am trying to port my custom asp 3.0 error page to asp.net. I want to disable debugging in my asp.net projects but i want to show the users a pretty page but that page should send me...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.