Showing posts with label servergetlasterror. Show all posts
Showing posts with label servergetlasterror. Show all posts

Thursday, March 22, 2012

Server.GetLastError

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?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" <anonymous@.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?
Since you are catching the exception, it is no longer an error.
bill
"VK" <anonymous@.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?
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?
>

Server.GetLastError inner exception

I've got a simple Application_OnError routine set up as a generic error trap
for web applications running on IIS server (VS 2003) that logs error info and
the text of the first inner exception. I've had exceptions thrown on occasion
that have up to three additional levels of inner exceptions. Is there a way
to drill down through each of these inner exceptions (using VB.NET) to
capture and report their message? The closest example I can find using C++
code that I can't get to work in what I think would be the equivalent VB code.

tia for any help,
SueSue,

I always just write the whole exception out. ex.ToString.

That captures everything. You can't have too much information when hunting
down a bug.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Sue" <Sue@.ojd.state.or.us> wrote in message
news:C44E2FDD-A349-494A-8115-1243D0F01DF3@.microsoft.com...
> I've got a simple Application_OnError routine set up as a generic error
> trap
> for web applications running on IIS server (VS 2003) that logs error info
> and
> the text of the first inner exception. I've had exceptions thrown on
> occasion
> that have up to three additional levels of inner exceptions. Is there a
> way
> to drill down through each of these inner exceptions (using VB.NET) to
> capture and report their message? The closest example I can find using C++
> code that I can't get to work in what I think would be the equivalent VB
> code.
> tia for any help,
> Sue
Oh geez - it can't be that simple <g>!

Thanks!
Sue

"S. Justin Gengo" wrote:

> Sue,
> I always just write the whole exception out. ex.ToString.
> That captures everything. You can't have too much information when hunting
> down a bug.
> --
> Sincerely,
> S. Justin Gengo, MCP
> Web Developer / Programmer
> www.aboutfortunate.com
> "Out of chaos comes order."
> Nietzsche
> "Sue" <Sue@.ojd.state.or.us> wrote in message
> news:C44E2FDD-A349-494A-8115-1243D0F01DF3@.microsoft.com...
> > I've got a simple Application_OnError routine set up as a generic error
> > trap
> > for web applications running on IIS server (VS 2003) that logs error info
> > and
> > the text of the first inner exception. I've had exceptions thrown on
> > occasion
> > that have up to three additional levels of inner exceptions. Is there a
> > way
> > to drill down through each of these inner exceptions (using VB.NET) to
> > capture and report their message? The closest example I can find using C++
> > code that I can't get to work in what I think would be the equivalent VB
> > code.
> > tia for any help,
> > Sue
>

Server.GetLastError

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?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" <anonymous@.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?
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" <anonymous@.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?
Since you are catching the exception, it is no longer an error.

bill

"VK" <anonymous@.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?
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?

Server.GetLastError

In my web.config I have:

<customErrors defaultRedirect="/error.aspx" mode="On"
in error.aspx I try to get the last error and do something with it:

Dim ex As Exception = Server.GetLastError

I get Nothing (ex = Nothing)

Is the reason for ex = Nothing that Server.GetLastError only gets
exceptions manually thrown?

If not how do I then access the last error?

/MOn Sat, 11 Jun 2005 12:35:15 -0500, hansiman <hansi@.hotmail.com> wrote:

> In my web.config I have:
> <customErrors defaultRedirect="/error.aspx" mode="On">
> in error.aspx I try to get the last error and do something with it:
> Dim ex As Exception = Server.GetLastError
> I get Nothing (ex = Nothing)
> Is the reason for ex = Nothing that Server.GetLastError only gets
> exceptions manually thrown?
> If not how do I then access the last error?
> /M

In essence the unhandled exception bubbles to the Page_Error level, and
then if not caught or cleared there, it continues to the Application_Error
(in global.asax). If you do not call Context.ClearError() at any point,
the built-in error handling then redirects to your error page you have
defined in your web.config. But after the redirect you've lost access to
the error.

A rather lengthy article with alternatives is here:

http://www.dotnetjohn.com/articles.aspx?articleid=42

basically if you want to display the error, you can call
Context.ClearError() after getting a reference to it, and either
Reponse.Write or redirect to some page of your own.

--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET

Server.GetLastError() return null

Try using the Application_Error() event in Global.asax.
The context of the error will be available here. Your
application is handling the error, and thus there is no
LastError to Get.

hth,
-Christopher
http://weblogs.asp.net/CFrazierUsing Application_Error() does work. Thank-you!!!

What's confusing though is that I am simply trying to work
through an example on page 292 in "Developing WEB
Applications with Microsoft Visual Basic .NET and Visual
C# .NET". This example works with Server.GetLastError().
Perhaps book is wrong?

>--Original Message--
>Try using the Application_Error() event in Global.asax.
>The context of the error will be available here. Your
>application is handling the error, and thus there is no
>LastError to Get.
>hth,
>-Christopher
>http://weblogs.asp.net/CFrazier
>.

Server.GetLastError() question.

Hi,

Does any1 can tell me how Server.GetLastError work? say i have a case where i have 2 concurrent exception thrown in, and i write a BasePage.cs to inherit into all my aspx.cs page. Then inside my BasePage.cs has a procedure called "protected override void OnError(EventArgs e)".My question is if 2 concurrent exception had occur,how does the SErver.GetlastError() know which is which?

The reason i ask this because inside my BasePage.OnError procedure i have a function to write error to my log file:


ApplicationLog.WriteError(Server.GetLastError());

and i m wondering how .NET handle this issues??

please help thank you.Well, as far as I'm aware (and note that I'm not as handy with exception handling as I'd like to be), the situation you describe can't occur--or at least is so rare that you might be concerned about Earth wobbling off its axis and wandering into Andromeda before it happens.

Is it possible for 2 errors to occur in exactly the same millisecond? Yes, I suppose so. I think, though, as a matter of 'bang for your buck' in terms of your own time spent, you might be better off to code as though you'll never see it.

Then, if by extremely remote chance it does occur, you can spend less time working out whose error is whose than you would've spent trying to anticipate the situation.

Of course, I may be all wet on this--anyone else have any comments?
u r right in away..but precaution better then cure..
anyway..i got it from unofficial source regarding Server.GetlastError();

according to him,"server.getlasterror is thread dependend" which means it "raised for the current request" and if it really happen that 2 concurrent exception is thrown,it will have "will have two different objects".

Well that was from a unofficial source..i m not sure about it i m still looking high n low about it...i spent whole nite looking for it and i m still looking..

Rgd,
Dorris
TheServer object is available from theHttpContext of each ASP.NET page request. Each page request is serviced by a single thread, available from a pool of only 25 worker threads. The chance of two out of 25 threads giving rise to a concurrent exception in the same millisecond is indeed extremely remote. (You are much more likely to cause a concurrency problem yourself, when writing to your application log.)

You might take heart from the introduction toUse Threads and Build Asynchronous Handlers in Your Server-Side Web Code:
"If you look at the documentation for ASP.NET, however, you will find very little information about threading requirements. Does this mean that all threading issues are solved and that ASP.NET developers can live carefree lives building .aspx pages and Microsoft® .NET Framework classes without cluttering their minds with concurrency issues? Well, yes, in most cases it does."

Remember that ASP.NET is a managed environment. It is designed to alleviate the need for developers to worry about most issues of threading, memory management, and so on.

I think you can leave the issue of worker threads to ASP.NET. You, the developer, have some responsibility when dealing with the IO threads ... and you should concentrate your efforts there. (In your case, reading and writing of your application log.)
Say, good answer.

So how're things down under? :)
thank you alot for the great advise.
You're welcome, Dorris.

Jeff, there's a mighty confused wanna-be developer down under ... I can't findApplication Design for Dummies in any bookstore :)
<g

Server.GetLastError() in a Web Farm

If I am handling errors in my Global.Asax Application_Error method and this
resides on a web farm and I am trying to trap the last exception that was
thrown, I assume that Server.GetLastError() will only check for the last
error on the current server. What if I was redirected via load balancing to
a different server. How do I accomidate for this?If an exception occured, how would you get redirected to a different server?

Exception Occurs
local error handling
page error handling
global error handling

there's no redirection happening...it all happens in a single thread...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/

"Daniel Williams" <Daniel Williams@.discussions.microsoft.com> wrote in
message news:79CDFA4C-0E4E-41D1-8F1E-AF3CF66DF570@.microsoft.com...
> If I am handling errors in my Global.Asax Application_Error method and
this
> resides on a web farm and I am trying to trap the last exception that was
> thrown, I assume that Server.GetLastError() will only check for the last
> error on the current server. What if I was redirected via load balancing
to
> a different server. How do I accomidate for this?

Server.GetLastError() in a Web Farm

If I am handling errors in my Global.Asax Application_Error method and this
resides on a web farm and I am trying to trap the last exception that was
thrown, I assume that Server.GetLastError() will only check for the last
error on the current server. What if I was redirected via load balancing to
a different server. How do I accomidate for this?If an exception occured, how would you get redirected to a different server?
Exception Occurs
local error handling
page error handling
global error handling
there's no redirection happening...it all happens in a single thread...
Karl
MY ASP.Net tutorials
http://www.openmymind.net/
"Daniel Williams" <Daniel Williams@.discussions.microsoft.com> wrote in
message news:79CDFA4C-0E4E-41D1-8F1E-AF3CF66DF570@.microsoft.com...
> If I am handling errors in my Global.Asax Application_Error method and
this
> resides on a web farm and I am trying to trap the last exception that was
> thrown, I assume that Server.GetLastError() will only check for the last
> error on the current server. What if I was redirected via load balancing
to
> a different server. How do I accomidate for this?

Server.GetLastError() and "Page-Level Error Pages"

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:
========
<%@dotnet.itags.org. Page language="c#" Codebehind="TestErrOnPage.aspx.cs"
AutoEventWireup="false" Inherits="myDummyTry.TestErrOnPage"
errorPage="ErrDefault1.aspx"%>
I have placed a button on the form and in the code behind just thrown an
Exception.
in ErrDefaulst1.aspx:
==============
private void Page_Load(object sender, System.EventArgs e)
{
if (Server.GetLastError() != null)
litError.Text="<h3>" + Server.GetLastError().Message + "</h3>";
else
litError.Text= "<h3>Unkown Error occured...</h3>";
Server.ClearError();
}
It is said, that is is possible to use the Server.GetLastError() in the
error page. However, it is always null.
Did I miss something?
Thanks,
JosI asked this.. and got answered by myself:) a few days ago..
My answer:
"Since you are redirected to another errorpage you loose the GetLatsetError
information, cause you are not on that page anymore.. BUT.. if you in
Global.aspx in Page_Eerror event (where it's still accessible) put
Server.GetLastError in a SESSION variable.. you can get it thru that on the
Errorpage .."
/Lars
"Jos Joye" <jose.joye@.KILLTHESPAMSbluewin.ch> skrev i meddelandet
news:usxcl$eeEHA.3132@.TK2MSFTNGP11.phx.gbl...
> 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:
> ========
> <%@. Page language="c#" Codebehind="TestErrOnPage.aspx.cs"
> AutoEventWireup="false" Inherits="myDummyTry.TestErrOnPage"
> errorPage="ErrDefault1.aspx"%>
> I have placed a button on the form and in the code behind just thrown an
> Exception.
>
> in ErrDefaulst1.aspx:
> ==============
> private void Page_Load(object sender, System.EventArgs e)
> {
> if (Server.GetLastError() != null)
> litError.Text="<h3>" + Server.GetLastError().Message + "</h3>";
> else
> litError.Text= "<h3>Unkown Error occured...</h3>";
> Server.ClearError();
> }
>
> It is said, that is is possible to use the Server.GetLastError() in the
> error page. However, it is always null.
> Did I miss something?
> Thanks,
> Jos
>
>
>
Thanks for the answer. However, I'm not too clear about it.
1) I thaught that the Page_Error() method has to be located in the Web class
and not in the Global.aspx. Did I misundertood what you explained me.
2) With the following:
<%@. Page language="c#" Codebehind="TestErrOnPage.aspx.cs"
AutoEventWireup="false" Inherits="myDummyTry.TestErrOnPage"
errorPage="ErrDefault1.aspx"%>
I was trying to get rid of the Page_Error() method and have a "kind of
automatic" redirection to the ErrDefault1.aspx page in case of error.
If I make usage of the Page_Error() method, I do not see the reason of
having the "errorPage="ErrDefault1.aspx"%>" statement in my aspx file.
Thanks,
Jos
"Lars Netzel" <[stop_spam]@.host.topdomain> wrote in message
news:%23kg7VEfeEHA.1764@.TK2MSFTNGP10.phx.gbl...
> I asked this.. and got answered by myself:) a few days ago..
> My answer:
> "Since you are redirected to another errorpage you loose the
GetLatsetError
> information, cause you are not on that page anymore.. BUT.. if you in
> Global.aspx in Page_Eerror event (where it's still accessible) put
> Server.GetLastError in a SESSION variable.. you can get it thru that on
the
> Errorpage .."
> /Lars
>
>
> "Jos Joye" <jose.joye@.KILLTHESPAMSbluewin.ch> skrev i meddelandet
> news:usxcl$eeEHA.3132@.TK2MSFTNGP11.phx.gbl...
>
Hi Jos,
I think Lars is suggesting having the exception handled, or at least its
details stored somewhere, in the Application_Error method in "Global.asax".
It may be that perhaps your exception page doesn't need to show the
details of the exception to the end user, although I, of course, don't
know the details of your scenario. If you're principally interested in
apologizing to the user and publishing the details of the exception
somewhere developers can get hold of it (event log, email, file etc) you
could use Microsoft Exception Management Application block from:
http://msdn.microsoft.com/library/d...-r
m.asp
You could get around adding errorPage and Page_Error() code your
individual aspx pages by also setting the default error redirect in your
webconfig <customErrors> section:
<customErrors mode="On" defaultRedirect="ErrorPage.aspx">
</customErrors>
(mode attribute is explained in the comments preceding the section in
web.config, "On" is likely to be necessary for testing and debugging if
you're serving the pages from your own machine)
In this way your page exception would not get handled on the page, would
propogate to the Global.asax handler, which would publish the exception
to whatever log your wanted, or perhaps store it in some accessible
storage, and *not* clear the error. Then the exception would propogate
to the defaultRedirect page in the web.config file, which could
apologize to the user and possibly use any persisted exception details.
This works quite neatly, although it is worth noting that any resources
not handled by the ASP.NET isapi filter (for instance any legacy asp
pages you may be obliged to include) will require additional handling.
There may well be faults with this method of handling things, and to be
honest it feels a little cumbersome defaulting to allowing the exception
to escalate to the application level, but I've certainly been obliged to
do it, and application level handling definitely should exist.
Hope I didn't deviate to far from your question!
Regards,
Duncan Kennedy.
Hi Duncan,
Thanks a lot for your really interesting reply.
I'm actually in the process of getting up to speed with ASP.NET (sorry for
the stupid questions ;-) )
I still have an open question related to this topic.
Say an exception is thrown in the Rendering part (preRendering/Rendering) of
the form.
This is actually out my scope. In that situation, if I do not have a
Page_Error() defined, how could I record the kind of error that occured.
If I understand it correctly, as soon as I get out of the Page scope, I will
not have the exception available anymore if I did not save it manually
somewhere (eg. : Session bag).
If I defined an Application_Error handler (in Global.asax) will I have this
information available?
Thanks,
Jos
"Duncan Kennedy" <youmustbespammingme.contractor1@.achilles.com> wrote in
message news:ucyw3ZgeEHA.1692@.tk2msftngp13.phx.gbl...
> Hi Jos,
> I think Lars is suggesting having the exception handled, or at least its
> details stored somewhere, in the Application_Error method in
"Global.asax".
> It may be that perhaps your exception page doesn't need to show the
> details of the exception to the end user, although I, of course, don't
> know the details of your scenario. If you're principally interested in
> apologizing to the user and publishing the details of the exception
> somewhere developers can get hold of it (event log, email, file etc) you
> could use Microsoft Exception Management Application block from:
>
http://msdn.microsoft.com/library/d...tml/emab-rm.asp[col
or=darkred]
>
> You could get around adding errorPage and Page_Error() code your
> individual aspx pages by also setting the default error redirect in your
> webconfig <customErrors> section:
> <customErrors mode="On" defaultRedirect="ErrorPage.aspx">
> </customErrors>
> (mode attribute is explained in the comments preceding the section in
> web.config, "On" is likely to be necessary for testing and debugging if
> you're serving the pages from your own machine)
> In this way your page exception would not get handled on the page, would
> propogate to the Global.asax handler, which would publish the exception
> to whatever log your wanted, or perhaps store it in some accessible
> storage, and *not* clear the error. Then the exception would propogate
> to the defaultRedirect page in the web.config file, which could
> apologize to the user and possibly use any persisted exception details.
> This works quite neatly, although it is worth noting that any resources
> not handled by the ASP.NET isapi filter (for instance any legacy asp
> pages you may be obliged to include) will require additional handling.
> There may well be faults with this method of handling things, and to be
> honest it feels a little cumbersome defaulting to allowing the exception
> to escalate to the application level, but I've certainly been obliged to
> do it, and application level handling definitely should exist.
> Hope I didn't deviate to far from your question!
>
> Regards,
>
> Duncan Kennedy.[/color]
Hi Jos,
I'm sorrier for the stupid answers...
If an exception either occurs on a page before a Page_Error() handler
has been assigned or is not cleared by by a Page_Error() method then the
application will throw a HttpUnhandledException exception, which will
contain in its "innerException" property the exception that was
unhandled on the page.
As such you should still have your page exception when you're coding at
the Application_Error level, it will just be wrapped in another
exception. You could persist this somewhere appropriate if you wanted
to keep it to give a verbose report to the user when you finally
transferred the scope to an error page (Server.Redirect, defaultError in
web.config etc).
I hope this helps, Michael O'Donovan pointed out the following url on
error handling in another thread which might be useful:
http://msdn.microsoft.com/asp.net/u...ustomErrors.asp
Best of luck,
Duncan.
Jos Joye wrote:
> Hi Duncan,
> Thanks a lot for your really interesting reply.
> I'm actually in the process of getting up to speed with ASP.NET (sorry for
> the stupid questions ;-) )
> I still have an open question related to this topic.
> Say an exception is thrown in the Rendering part (preRendering/Rendering)
of
> the form.
> This is actually out my scope. In that situation, if I do not have a
> Page_Error() defined, how could I record the kind of error that occured.
> If I understand it correctly, as soon as I get out of the Page scope, I wi
ll
> not have the exception available anymore if I did not save it manually
> somewhere (eg. : Session bag).
> If I defined an Application_Error handler (in Global.asax) will I have thi
s
> information available?
> Thanks,
> Jos
>
> "Duncan Kennedy" <youmustbespammingme.contractor1@.achilles.com> wrote in
> message news:ucyw3ZgeEHA.1692@.tk2msftngp13.phx.gbl...
>
> "Global.asax".
>
> http://msdn.microsoft.com/library/d...
-rm.asp
>
>
>
Thanks a lot for the explanation and for the great reference. I think that
I'm pretty clear on this topic now :-))
Jos
"Duncan Kennedy" <youmustbespammingme.contractor1@.achilles.com> a crit dans
le message de news:euOxvEjeEHA.2812@.tk2msftngp13.phx.gbl...
> Hi Jos,
> I'm sorrier for the stupid answers...
> If an exception either occurs on a page before a Page_Error() handler
> has been assigned or is not cleared by by a Page_Error() method then the
> application will throw a HttpUnhandledException exception, which will
> contain in its "innerException" property the exception that was
> unhandled on the page.
> As such you should still have your page exception when you're coding at
> the Application_Error level, it will just be wrapped in another
> exception. You could persist this somewhere appropriate if you wanted
> to keep it to give a verbose report to the user when you finally
> transferred the scope to an error page (Server.Redirect, defaultError in
> web.config etc).
> I hope this helps, Michael O'Donovan pointed out the following url on
> error handling in another thread which might be useful:
>
http://msdn.microsoft.com/asp.net/u...ustomErrors.asp
> Best of luck,
> Duncan.
>
>
> Jos Joye wrote:
for
(preRendering/Rendering) of
will
this
http://msdn.microsoft.com/library/d...tml/emab-rm.asp[col
or=darkred]

Server.GetLastError() and "Page-Level Error Pages"

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:
========
<%@dotnet.itags.org. Page language="c#" Codebehind="TestErrOnPage.aspx.cs"
AutoEventWireup="false" Inherits="myDummyTry.TestErrOnPage"
errorPage="ErrDefault1.aspx"%
I have placed a button on the form and in the code behind just thrown an
Exception.

in ErrDefaulst1.aspx:
==============

private void Page_Load(object sender, System.EventArgs e)
{
if (Server.GetLastError() != null)
litError.Text="<h3>" + Server.GetLastError().Message + "</h3>";
else
litError.Text= "<h3>Unkown Error occured...</h3>";
Server.ClearError();
}

It is said, that is is possible to use the Server.GetLastError() in the
error page. However, it is always null.
Did I miss something?

Thanks,
JosI asked this.. and got answered by myself:) a few days ago..

My answer:

"Since you are redirected to another errorpage you loose the GetLatsetError
information, cause you are not on that page anymore.. BUT.. if you in
Global.aspx in Page_Eerror event (where it's still accessible) put
Server.GetLastError in a SESSION variable.. you can get it thru that on the
Errorpage .."

/Lars

"Jos Joye" <jose.joye@.KILLTHESPAMSbluewin.ch> skrev i meddelandet
news:usxcl$eeEHA.3132@.TK2MSFTNGP11.phx.gbl...
> 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:
> ========
> <%@. Page language="c#" Codebehind="TestErrOnPage.aspx.cs"
> AutoEventWireup="false" Inherits="myDummyTry.TestErrOnPage"
> errorPage="ErrDefault1.aspx"%>
> I have placed a button on the form and in the code behind just thrown an
> Exception.
>
> in ErrDefaulst1.aspx:
> ==============
> private void Page_Load(object sender, System.EventArgs e)
> {
> if (Server.GetLastError() != null)
> litError.Text="<h3>" + Server.GetLastError().Message + "</h3>";
> else
> litError.Text= "<h3>Unkown Error occured...</h3>";
> Server.ClearError();
> }
>
> It is said, that is is possible to use the Server.GetLastError() in the
> error page. However, it is always null.
> Did I miss something?
> Thanks,
> Jos
>
Thanks for the answer. However, I'm not too clear about it.

1) I thaught that the Page_Error() method has to be located in the Web class
and not in the Global.aspx. Did I misundertood what you explained me.

2) With the following:
<%@. Page language="c#" Codebehind="TestErrOnPage.aspx.cs"
AutoEventWireup="false" Inherits="myDummyTry.TestErrOnPage"
errorPage="ErrDefault1.aspx"%>
I was trying to get rid of the Page_Error() method and have a "kind of
automatic" redirection to the ErrDefault1.aspx page in case of error.
If I make usage of the Page_Error() method, I do not see the reason of
having the "errorPage="ErrDefault1.aspx"%>" statement in my aspx file.

Thanks,
Jos

"Lars Netzel" <[stop_spam]@.host.topdomain> wrote in message
news:%23kg7VEfeEHA.1764@.TK2MSFTNGP10.phx.gbl...
> I asked this.. and got answered by myself:) a few days ago..
> My answer:
> "Since you are redirected to another errorpage you loose the
GetLatsetError
> information, cause you are not on that page anymore.. BUT.. if you in
> Global.aspx in Page_Eerror event (where it's still accessible) put
> Server.GetLastError in a SESSION variable.. you can get it thru that on
the
> Errorpage .."
> /Lars
>
>
> "Jos Joye" <jose.joye@.KILLTHESPAMSbluewin.ch> skrev i meddelandet
> news:usxcl$eeEHA.3132@.TK2MSFTNGP11.phx.gbl...
> > 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:
> > ========
> > <%@. Page language="c#" Codebehind="TestErrOnPage.aspx.cs"
> > AutoEventWireup="false" Inherits="myDummyTry.TestErrOnPage"
> > errorPage="ErrDefault1.aspx"%>
> > I have placed a button on the form and in the code behind just thrown an
> > Exception.
> > in ErrDefaulst1.aspx:
> > ==============
> > private void Page_Load(object sender, System.EventArgs e)
> > {
> > if (Server.GetLastError() != null)
> > litError.Text="<h3>" + Server.GetLastError().Message + "</h3>";
> > else
> > litError.Text= "<h3>Unkown Error occured...</h3>";
> > Server.ClearError();
> > }
> > It is said, that is is possible to use the Server.GetLastError() in the
> > error page. However, it is always null.
> > Did I miss something?
> > Thanks,
> > Jos
Hi Jos,

I think Lars is suggesting having the exception handled, or at least its
details stored somewhere, in the Application_Error method in "Global.asax".

It may be that perhaps your exception page doesn't need to show the
details of the exception to the end user, although I, of course, don't
know the details of your scenario. If you're principally interested in
apologizing to the user and publishing the details of the exception
somewhere developers can get hold of it (event log, email, file etc) you
could use Microsoft Exception Management Application block from:

http://msdn.microsoft.com/library/d...tml/emab-rm.asp

You could get around adding errorPage and Page_Error() code your
individual aspx pages by also setting the default error redirect in your
webconfig <customErrors> section:

<customErrors mode="On" defaultRedirect="ErrorPage.aspx">
</customErrors
(mode attribute is explained in the comments preceding the section in
web.config, "On" is likely to be necessary for testing and debugging if
you're serving the pages from your own machine)

In this way your page exception would not get handled on the page, would
propogate to the Global.asax handler, which would publish the exception
to whatever log your wanted, or perhaps store it in some accessible
storage, and *not* clear the error. Then the exception would propogate
to the defaultRedirect page in the web.config file, which could
apologize to the user and possibly use any persisted exception details.

This works quite neatly, although it is worth noting that any resources
not handled by the ASP.NET isapi filter (for instance any legacy asp
pages you may be obliged to include) will require additional handling.

There may well be faults with this method of handling things, and to be
honest it feels a little cumbersome defaulting to allowing the exception
to escalate to the application level, but I've certainly been obliged to
do it, and application level handling definitely should exist.

Hope I didn't deviate to far from your question!

Regards,

Duncan Kennedy.
Hi Duncan,

Thanks a lot for your really interesting reply.
I'm actually in the process of getting up to speed with ASP.NET (sorry for
the stupid questions ;-) )

I still have an open question related to this topic.
Say an exception is thrown in the Rendering part (preRendering/Rendering) of
the form.
This is actually out my scope. In that situation, if I do not have a
Page_Error() defined, how could I record the kind of error that occured.
If I understand it correctly, as soon as I get out of the Page scope, I will
not have the exception available anymore if I did not save it manually
somewhere (eg. : Session bag).

If I defined an Application_Error handler (in Global.asax) will I have this
information available?

Thanks,
Jos

"Duncan Kennedy" <youmustbespammingme.contractor1@.achilles.com> wrote in
message news:ucyw3ZgeEHA.1692@.tk2msftngp13.phx.gbl...
> Hi Jos,
> I think Lars is suggesting having the exception handled, or at least its
> details stored somewhere, in the Application_Error method in
"Global.asax".
> It may be that perhaps your exception page doesn't need to show the
> details of the exception to the end user, although I, of course, don't
> know the details of your scenario. If you're principally interested in
> apologizing to the user and publishing the details of the exception
> somewhere developers can get hold of it (event log, email, file etc) you
> could use Microsoft Exception Management Application block from:
>
http://msdn.microsoft.com/library/d...tml/emab-rm.asp
>
> You could get around adding errorPage and Page_Error() code your
> individual aspx pages by also setting the default error redirect in your
> webconfig <customErrors> section:
> <customErrors mode="On" defaultRedirect="ErrorPage.aspx">
> </customErrors>
> (mode attribute is explained in the comments preceding the section in
> web.config, "On" is likely to be necessary for testing and debugging if
> you're serving the pages from your own machine)
> In this way your page exception would not get handled on the page, would
> propogate to the Global.asax handler, which would publish the exception
> to whatever log your wanted, or perhaps store it in some accessible
> storage, and *not* clear the error. Then the exception would propogate
> to the defaultRedirect page in the web.config file, which could
> apologize to the user and possibly use any persisted exception details.
> This works quite neatly, although it is worth noting that any resources
> not handled by the ASP.NET isapi filter (for instance any legacy asp
> pages you may be obliged to include) will require additional handling.
> There may well be faults with this method of handling things, and to be
> honest it feels a little cumbersome defaulting to allowing the exception
> to escalate to the application level, but I've certainly been obliged to
> do it, and application level handling definitely should exist.
> Hope I didn't deviate to far from your question!
>
> Regards,
>
> Duncan Kennedy.
Hi Jos,

I'm sorrier for the stupid answers...

If an exception either occurs on a page before a Page_Error() handler
has been assigned or is not cleared by by a Page_Error() method then the
application will throw a HttpUnhandledException exception, which will
contain in its "innerException" property the exception that was
unhandled on the page.

As such you should still have your page exception when you're coding at
the Application_Error level, it will just be wrapped in another
exception. You could persist this somewhere appropriate if you wanted
to keep it to give a verbose report to the user when you finally
transferred the scope to an error page (Server.Redirect, defaultError in
web.config etc).

I hope this helps, Michael O'Donovan pointed out the following url on
error handling in another thread which might be useful:

http://msdn.microsoft.com/asp.net/u...ustomErrors.asp

Best of luck,

Duncan.

Jos Joye wrote:
> Hi Duncan,
> Thanks a lot for your really interesting reply.
> I'm actually in the process of getting up to speed with ASP.NET (sorry for
> the stupid questions ;-) )
> I still have an open question related to this topic.
> Say an exception is thrown in the Rendering part (preRendering/Rendering) of
> the form.
> This is actually out my scope. In that situation, if I do not have a
> Page_Error() defined, how could I record the kind of error that occured.
> If I understand it correctly, as soon as I get out of the Page scope, I will
> not have the exception available anymore if I did not save it manually
> somewhere (eg. : Session bag).
> If I defined an Application_Error handler (in Global.asax) will I have this
> information available?
> Thanks,
> Jos
>
> "Duncan Kennedy" <youmustbespammingme.contractor1@.achilles.com> wrote in
> message news:ucyw3ZgeEHA.1692@.tk2msftngp13.phx.gbl...
>>Hi Jos,
>>
>>I think Lars is suggesting having the exception handled, or at least its
>>details stored somewhere, in the Application_Error method in
> "Global.asax".
>>It may be that perhaps your exception page doesn't need to show the
>>details of the exception to the end user, although I, of course, don't
>>know the details of your scenario. If you're principally interested in
>>apologizing to the user and publishing the details of the exception
>>somewhere developers can get hold of it (event log, email, file etc) you
>>could use Microsoft Exception Management Application block from:
>>
>>
> http://msdn.microsoft.com/library/d...tml/emab-rm.asp
>>
>>You could get around adding errorPage and Page_Error() code your
>>individual aspx pages by also setting the default error redirect in your
>>webconfig <customErrors> section:
>>
>> <customErrors mode="On" defaultRedirect="ErrorPage.aspx">
>> </customErrors>
>>
>>(mode attribute is explained in the comments preceding the section in
>>web.config, "On" is likely to be necessary for testing and debugging if
>>you're serving the pages from your own machine)
>>
>>In this way your page exception would not get handled on the page, would
>>propogate to the Global.asax handler, which would publish the exception
>>to whatever log your wanted, or perhaps store it in some accessible
>>storage, and *not* clear the error. Then the exception would propogate
>>to the defaultRedirect page in the web.config file, which could
>>apologize to the user and possibly use any persisted exception details.
>>
>>This works quite neatly, although it is worth noting that any resources
>>not handled by the ASP.NET isapi filter (for instance any legacy asp
>>pages you may be obliged to include) will require additional handling.
>>
>>There may well be faults with this method of handling things, and to be
>>honest it feels a little cumbersome defaulting to allowing the exception
>>to escalate to the application level, but I've certainly been obliged to
>>do it, and application level handling definitely should exist.
>>
>>Hope I didn't deviate to far from your question!
>>
>>
>>Regards,
>>
>>
>>Duncan Kennedy.
>
Thanks a lot for the explanation and for the great reference. I think that
I'm pretty clear on this topic now :-))
Jos
"Duncan Kennedy" <youmustbespammingme.contractor1@.achilles.com> a crit dans
le message de news:euOxvEjeEHA.2812@.tk2msftngp13.phx.gbl...
> Hi Jos,
> I'm sorrier for the stupid answers...
> If an exception either occurs on a page before a Page_Error() handler
> has been assigned or is not cleared by by a Page_Error() method then the
> application will throw a HttpUnhandledException exception, which will
> contain in its "innerException" property the exception that was
> unhandled on the page.
> As such you should still have your page exception when you're coding at
> the Application_Error level, it will just be wrapped in another
> exception. You could persist this somewhere appropriate if you wanted
> to keep it to give a verbose report to the user when you finally
> transferred the scope to an error page (Server.Redirect, defaultError in
> web.config etc).
> I hope this helps, Michael O'Donovan pointed out the following url on
> error handling in another thread which might be useful:
>
http://msdn.microsoft.com/asp.net/u...ustomErrors.asp
> Best of luck,
> Duncan.
>
>
> Jos Joye wrote:
> > Hi Duncan,
> > Thanks a lot for your really interesting reply.
> > I'm actually in the process of getting up to speed with ASP.NET (sorry
for
> > the stupid questions ;-) )
> > I still have an open question related to this topic.
> > Say an exception is thrown in the Rendering part
(preRendering/Rendering) of
> > the form.
> > This is actually out my scope. In that situation, if I do not have a
> > Page_Error() defined, how could I record the kind of error that occured.
> > If I understand it correctly, as soon as I get out of the Page scope, I
will
> > not have the exception available anymore if I did not save it manually
> > somewhere (eg. : Session bag).
> > If I defined an Application_Error handler (in Global.asax) will I have
this
> > information available?
> > Thanks,
> > Jos
> > "Duncan Kennedy" <youmustbespammingme.contractor1@.achilles.com> wrote in
> > message news:ucyw3ZgeEHA.1692@.tk2msftngp13.phx.gbl...
> >>Hi Jos,
> >>
> >>I think Lars is suggesting having the exception handled, or at least its
> >>details stored somewhere, in the Application_Error method in
> > "Global.asax".
> >>It may be that perhaps your exception page doesn't need to show the
> >>details of the exception to the end user, although I, of course, don't
> >>know the details of your scenario. If you're principally interested in
> >>apologizing to the user and publishing the details of the exception
> >>somewhere developers can get hold of it (event log, email, file etc) you
> >>could use Microsoft Exception Management Application block from:
> >>
> >>
http://msdn.microsoft.com/library/d...tml/emab-rm.asp
> >>
> >>You could get around adding errorPage and Page_Error() code your
> >>individual aspx pages by also setting the default error redirect in your
> >>webconfig <customErrors> section:
> >>
> >> <customErrors mode="On" defaultRedirect="ErrorPage.aspx">
> >> </customErrors>
> >>
> >>(mode attribute is explained in the comments preceding the section in
> >>web.config, "On" is likely to be necessary for testing and debugging if
> >>you're serving the pages from your own machine)
> >>
> >>In this way your page exception would not get handled on the page, would
> >>propogate to the Global.asax handler, which would publish the exception
> >>to whatever log your wanted, or perhaps store it in some accessible
> >>storage, and *not* clear the error. Then the exception would propogate
> >>to the defaultRedirect page in the web.config file, which could
> >>apologize to the user and possibly use any persisted exception details.
> >>
> >>This works quite neatly, although it is worth noting that any resources
> >>not handled by the ASP.NET isapi filter (for instance any legacy asp
> >>pages you may be obliged to include) will require additional handling.
> >>
> >>There may well be faults with this method of handling things, and to be
> >>honest it feels a little cumbersome defaulting to allowing the exception
> >>to escalate to the application level, but I've certainly been obliged to
> >>do it, and application level handling definitely should exist.
> >>
> >>Hope I didn't deviate to far from your question!
> >>
> >>
> >>Regards,
> >>
> >>
> >>Duncan Kennedy.

Server.GetLastError()

I am using Server.GetLastError() to get error details for logging, which is fine, but what I really need is the actual line of code that caused the error.

If you don't have a custom error page, it shows you the actual line that caused the error but I can't seem to find it in the Server.GetLastError() object.

I have noticed that the following code gets me the name of the procedure in which the error occurred:
appException = Server.GetLastError()
MsgBox(appException.TargetSite.Name)

But not the actual line of code that triggered the error.Look at the properties of the exception named SOURCE, MESSAGE, STACKTRACE.
I have. They don't give me what I need.
Sorry, I haven't been able to find anything for this. It seems like the line-of-code that's erroring out is something that the framework keeps to itself, for security purposes? :confused:
Oh dear...:(
One thing to make sure of:

Did you compile the Application to DEBUG and no RELEASE? Lines of code are not available in release compiles. Also, make sure that the PDB file got uploaded. This is the debug trace hook file.

All that said, it is entirely possible it will not keep the line number around because you have to execute this code (Server.GetLastError()) AFTER the page exception occurs and is thrown, which has probably cleared the exception stack already.