473,386 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to check object exists if it raises error?

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
4 2557
HttpContext.Current?

Marc

Jun 15 '07 #2
You might get a good response if you repost this question to
microsoft.public.dotnet.framework.aspnet

-HTH
Jun 15 '07 #3
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter | last post by:
I have a program which was working fine. In it a function works out the number of .jpg files in a specified folder. I then realised that I needed to make sure the folder exists first & added the...
33
by: Steven Bethard | last post by:
Is there a good way to determine if an object is a numeric type? Generally, I avoid type-checks in favor of try/except blocks, but I'm not sure what to do in this case: def f(i): ... if x < i:...
2
by: Jonathan | last post by:
I am looking for a simple way to check if a database table exists. I keep getting advice to use "Try.. Catch" and other error handling methods, but I obviously don't want to have to display an...
2
by: Mike | last post by:
I´ve got a number of SPAN elements named "mySpan1", "mySpan2", "mySpan3" etc, and want to set their "style.display" to "inline". This works (only needs to work on IE5.5+): for (var x = 1; x <...
11
by: CWaldman | last post by:
How can I check if a function exists in any of the scripts on my page? I prefer if it can work for both Javascript & VBScript.
2
by: hb | last post by:
Hi, I need to check if a node exists in a XmlDocument object before to use SelectSingleNode() to process. I tried with if(XmlNode.SelectSingleNode("ABC")!=null) But this only works when the...
5
by: Notgiven | last post by:
You want to check scheduling conflicts and you have a record like: appointments(table): apptID beginningDate endingDate beginningTime endingTime It's easy enough to check if a time is...
9
by: Chris Ashley | last post by:
How do I check if an object exists in Javascript? EG: if (document.getElementById("product").value == null) { prodValue = ""; } else {
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.