Reputation: 20213
ok, I feel stupid asking this question, and forgive my poor research techniques, but...
using the example
if( obj.attr1.attr2.attr3 == 'constant' ) return; else if( condition2 ) ...
if obj.attr1
is undefined, the javascript engine throws an error.
what is the error that is thrown? is it universally defined?
is is possible to globally trap this error?
if trapped, is it possible for the next line condition2
to be executed?
to clarify: the error is raised because trying to get attribute of undefined. is there any way to know that this is the error being raised? is it in some table of standard javascript error messages?
and second, having trapped the error upstream, is it possible for program to flow uninterruptedly?
Upvotes: 4
Views: 975
Reputation: 30500
It's possible to trap this error with a try/catch block:
try{
if( obj.attr1.attr2.attr3 == 'constant' ) {
alert("test");
}
}
catch(e)
{
alert(e.Message);
}
The exception gives you the following:
description "'obj' is undefined" String
message "'obj' is undefined" String
name "TypeError" String
number -2146823279 Number
Upvotes: 2
Reputation: 338228
Usually this is solved gracefully by not blindly assuming that something is there.
if( obj.attr1 && obj.attr1.attr2 && obj.attr1.attr2.attr3 == 'constant' )
Other than that, you can write a try/catch statement that catches the exception here, but using structured exception handling to direct the normal program flow is frowned upon and should be avoided.
Upvotes: 0