473,746 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Server.GetLastE rror

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.GetLastE rror.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 4132
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_Err or event has to be raised to access it. Once one of those
events is raised Server.GetLastE rror 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.EventArg s) Handles MyBase.Load
Dim zero As Integer = 0
Dim err As Double = 10 / CType("x", Integer)
End Sub

Private Sub Page_Error(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles MyBase.Error
Response.Write( Server.GetLastE rror.Message)
End Sub

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

Private Sub Page_Error(ByVa l sender As Object, ByVal e As System.EventArg s)
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.EventArg s) Handles MyBase.Load
Response.Write( Server.GetLastE rror.Message)
End Sub

"VK" <an*******@disc ussions.microso ft.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.GetLastE rror.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_Err or event has to be raised to access it. Once one of those
events is raised Server.GetLastE rror 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.EventArg s) Handles MyBase.Load
Dim zero As Integer = 0
Dim err As Double = 10 / CType("x", Integer)
End Sub

Private Sub Page_Error(ByVa l sender As Object, ByVal e As System.EventArg s)
Handles MyBase.Error
Response.Write( Server.GetLastE rror.Message)
End Sub

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

Private Sub Page_Error(ByVa l sender As Object, ByVal e As System.EventArg s)
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.EventArg s) Handles MyBase.Load
Response.Write( Server.GetLastE rror.Message)
End Sub

"VK" <an*******@disc ussions.microso ft.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.GetLastE rror.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*******@disc ussions.microso ft.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.GetLastE rror.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.GetLastE rror.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
2941
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 = CreateObject("ADODB.Connectiom") Err.Number shows 429, which is normal. But Server.GetLastError reports no error! I would like to have line numbers and the line of source code, along with
3
8312
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
2132
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 lblErrormessage.Text += "<br>" & Server.GetLastError.Source Catch ex As Exception
6
4603
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 form: ========
3
6043
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 tried to remove the code-behind and add the <script language="C#" runat="server"> but that fails as well. The code is simply:
7
9009
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 can encode something using: C#: System.Security.Cryptography (to encode) and
7
3794
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 my cs file, but when I try and type the following line... Exception objErr = Server.GetLastError().GetBaseException();
2
4259
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 the applicationOnError code runs in Global.asax and hands it off to my custom error page but theres no info in the exception returned by Server.Getlasterror. The type of this exception is just HttpUnhandledException (which is obvious,
2
12212
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 an email with the details of the error. On classic asp i just went to iis and mapped the 500 error code to error.asp, which does this: set objError = Server.getLastError()
0
8974
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9350
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9218
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6060
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4586
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4836
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.