Thursday, March 22, 2012

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

0 comments:

Post a Comment