473,834 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById('indic ator');

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.getEle mentById('indic ator');

}
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 4944
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.getEle mentById('indic ator');

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.getEle mentById('indic ator');
} 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.getEle mentById('indic ator');
if (indicator && typeof indicator.inner HTML == '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.getEle mentById('indic ator');
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
3559
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 eh() { do something } }
11
2895
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 return code (not generating error/exception) so it is more compatible with other programming language.
6
1716
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, creating an unhandled exception inside "theUI". Would anybody know why this is happening? I thought the general rule for exceptions handling was the exception is passed up the stack until the application handles it or the program terminates? ...
11
5609
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
6660
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 length is 642 bytes ( I have appended it to the end of this message). I suspect that the reason may have something to do with an incorrect declaration of which class to de-serialize to. In the attached code I substituted @@@@@@@ in the code below with...
1
3034
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 having difficulties figuring out the best way to handle (catch) exceptions in the different layers and then propagating those errors back up through the call stack to ultimately display something to the end-user. Note this is an intranet...
132
5647
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 hard-to-find bugs. http://distributed-software.blogspot.com/2007/01/c-exception-handling-is-defective.html Regards, zorabi@ZHMicro.com http://www.zhmicro.com http://distributed-software.blogspot.com
6
3396
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 catching the exception in Win32Release mode.
5
1936
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 simply this - how can a function determine whether or not it's being called in handling of an exception (ie. there is an "active" exception, and somewhere upstream on the stack there is an except: block statement)? Depending on what you read,...
0
9799
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10795
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10512
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7760
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6957
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5627
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3981
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.