How to check object exists if it raises error?

M

Mark Berry

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

}
 
B

Bob Johnson

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

-HTH
 
M

Mark Berry

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
 
M

Mark Berry

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/library/system.web.httpapplication.request(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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top