473,387 Members | 1,528 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,387 software developers and data experts.

Exception Handling for Undefined element while creating its object

Hi All,

Is is possible to catch the error of an undefined element while
creating an object for it.

Consider we are not having an element with id indicator but we
are trying to make the object for it

indicator = document.getElementById('indicator');

Though we dont have any element like 'indicator' we wont get any
error

but if we try like this

indicator.style.innerHTML = 'Loading'

which throws the error "Indicator has no Properties"
Is it possible to catch the error while creating an object
itself
(ie)

try
{
indicator = document.getElementById('indicator');

}
catch(e)
{
//No Error

}
but

try
{
indicator.style.innerHTML = 'Loading'
}
catch(e)
{
// Here we get the error

}

But I need to cache the error while Creating the object itself , ie
If I try to create an object for an undefined element the error
should be thrown.

Is it posible.

Thanks & Regards
Moses

Feb 23 '07 #1
2 4916
On Feb 23, 2:33 pm, "Moses" <mosesdinaka...@gmail.comwrote:
Hi All,

Is is possible to catch the error of an undefined element while
creating an object for it.

Consider we are not having an element with id indicator but we
are trying to make the object for it

indicator = document.getElementById('indicator');

Though we dont have any element like 'indicator' we wont get any
error
If no element with id="indicator" exists, then the variable indicator
should be set to null.

<URL: http://www.w3.org/TR/DOM-Level-2-Cor...ml#ID-getElBId >

but if we try like this

indicator.style.innerHTML = 'Loading'

which throws the error "Indicator has no Properties"
Because you are trying to do something with a property of an object
that doesn't exist. Even if indicator is a reference to DOM element,
its style object doesn't have an innerHTML property, that is a
property of the element itself - maybe.
>
Is it possible to catch the error while creating an object
itself

(ie)
You me e.g.
>
try {
indicator = document.getElementById('indicator');
} catch(e) {
//No Error
It will enter the catch block if there *is* an error.
}

but

try {
indicator.style.innerHTML = 'Loading'
} catch(e) {
// Here we get the error
}

But I need to cache the error while Creating the object itself , ie
If I try to create an object for an undefined element the error
should be thrown.
You're confused. If I understand your question (and it's very likely
I don't), the error occurs because you are incorrectly using
innerHTML.

>
Is it posible.
Yes, test the return value of getElementById to ensure it returns an
object. Then test that the properties of that object are what you
expect before you try to access properties of the properties...

var indicator = document.getElementById('indicator');
if (indicator && typeof indicator.innerHTML == 'string') {
// do something with indicator's innerHTML property
}
--
Rob

Feb 23 '07 #2
Hi,

Though I have confused U have replied me what I want,

var indicator = document.getElementById('indicator');
if (!indicator)
{
throw 'Please Create the indicator Element';
}

Thank you.

Moses

Feb 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Xeon | last post by:
Hi, Is there anyway to set a custom error handler which is actually a method of a class? i.e. setting the method eh() of class foo as error handler in the snippet below. class foo { function...
11
by: adi | last post by:
Dear all, This is more like a theoretical or conceptual question: which is better, using exception or return code for a .NET component? I had created a COM object (using VB6), which uses...
6
by: Nick Reeves | last post by:
While developing the below code I noticed that exceptions were not working as expected. An exception is thrown inside an XML import DLL and not being caught inside the Main() code fragment,...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
9
by: MR | last post by:
I get the following Exception "The data at the root level is invalid. Line 1, position 642" whenever I try to deserialize an incoming SOAP message. The incoming message is formed well and its...
1
by: metsys | last post by:
We have an ASP.NET 2.0 (C#) application that is divided into multiple layers. The multiple layers come from having a web project and 2 different class library projects in the same solution. I'm...
132
by: Zorro | last post by:
The simplicity of stack unraveling of C++ is not without defective consequences. The following article points to C++ examples showing the defects. An engineer aware of defects can avoid...
6
by: Mohan | last post by:
Hi, I am learning the Exception Handling in C++. I wrote a small program using Exception Handling. I am using Vistual Studio 6. It is working fine in Win32 Debug build, but it is not...
5
by: NeBlackCat (lists) | last post by:
Hello everybody - my first post! And it may be the most monumentally stupid question ever asked, but I just can't see an answer after several hours experimenting, searching and reading. It's...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.