By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,739 Members | 2,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,739 IT Pros & Developers. It's quick & easy.

How to check object exists if it raises error?

P: n/a
Hi,

I'm working on my "last resort" error block for a web application.

If the error occurs after a Request has been made, I want to show the URL.
If the Request object is not available, I'll skip it.

I thought I could check for the existence of an object by comparing to null.
However, in the global.asax code below, the "if (Request == null)" line
throws the error "Request is not available in this context".

Is there a way to check whether Request is available before referencing it?

Thanks,

Mark
protected void Application_Start(object sender, EventArgs e)
{
throw new Exception(); // test getting an error before a page
displays
}

protected void Application_Error(object sender, EventArgs e)
{
Exception exc = Server.GetLastError().GetBaseException();

string message;
if (Request == null)
{
message = "Unhandled error caught in Application_Error event
as a last resort" +
"\nError Message:" + exc.Message.ToString();
}
else
{
message = "Unhandled error caught in Application_Error event
as a last resort" +
"\nError on Page: " + Request.Url.ToString() +
"\nError Message:" + exc.Message.ToString();
}

// Display and/or log message

}
Jun 15 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
HttpContext.Current?

Marc

Jun 15 '07 #2

P: n/a
You might get a good response if you repost this question to
microsoft.public.dotnet.framework.aspnet

-HTH
Jun 15 '07 #3

P: n/a
You're right, there are other ways to get to the data. Apparently the object
I am trying to use (System.Web.HttpApplication.Request) is initialized later
than others. So in the end, I'll use a different object.

However, after encounrtering this problem, I think I still need to allow for
the possibility that whatever object I want to reference is not there.

Mark

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
HttpContext.Current?

Marc

Jun 15 '07 #4

P: n/a
Thanks for the reference, Bob. Marc pointed out a workaround in the ASP.NET
framework. At this point, I'm looking for a more general answer.

Here is the definition of HttpApplication.Request:

http://msdn2.microsoft.com/en-us/lib...st(VS.80).aspx

Request is a property that returns the HttpRequest that the application is
processing. If HttpRequest is null, it throws an HttpException.

So my question is, when an object is defined this way, is there any way to
check for the existence of the object without getting an exception?

Mark

"Bob Johnson" <A@B.COMwrote in message
news:OX**************@TK2MSFTNGP03.phx.gbl...
You might get a good response if you repost this question to
microsoft.public.dotnet.framework.aspnet

-HTH

Jun 15 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.