0

When i try this code in javascript, why doesn't it catch the error on the misspelling on XMLHttpRequest?

  try {
   var x= new XMLHdfattpRequest();
  } catch (e){
   alert(e);
  }
8
  • 10
    in what browser? Commented Nov 17, 2010 at 17:38
  • @steve Could be that an earlier error is thrown so that the above code is not executed. Commented Nov 17, 2010 at 17:41
  • 1
    It's getting caught in FF testing using FireBug. Commented Nov 17, 2010 at 17:42
  • There are some things in js code, such as misspellings or things of that nature (can't remember all of the causes right now) that just make the Javascript "crap out" without error or anything. Could this be one of those things? Does IE or Firefox show that there are errors on the page (ones that would not be the same as errors you get while running)? Commented Nov 17, 2010 at 17:49
  • @Mike I don't think that this applies in this case. The above statement gets the value of the XMLHdfattpRequest identifier (which is undefined) and then tries to invoke (call) it via (). Since the undefined value cannot be called (only function objects can), an error will be thrown. Commented Nov 17, 2010 at 17:57

3 Answers 3

1

I've ran this code in Firefox, IE,Chrome and they all caught the error so I assume you mean catch the miss spelling. The reason it wasn't caught is because standard JavaScript is not statically validated like languages such as C#. When the code is executed the name XMLHdfattpRequest has to be checked for in the surrounding lexical scope. If it isn't found it becomes an unresolvable reference. When you try to new up an unresolvable reference that is when a reference error occurs.

Sign up to request clarification or add additional context in comments.

5 Comments

@Chaos I don't think that the specification mentions "unresolvable references". If the name foo is not found in the current scope chain, then the undefined value is returned. foo is undefined and calling foo (as in foo()) will throw an error.
@Šime Vidas - You need to dig a bit deeper. Read: 10.2.1.2 -> 11.2.2 -> 8.7.1
@Chaos +1 You're right. In detail, these steps are taken: 11.2.2 (The new Operator) -> 11.1.2 (Identifier Reference) -> 10.3.1 (Identifier Resolution) -> 10.2.2.1 (GetIdentifierReference) This will return a value of type Reference whose base value is undefined. Now, we return to the second step in 11.2.2 (The new Operator) -> 8.7.1 (GetValue) Here a ReferenceError is thrown.
@Chaos, @Sime Vidas - The chapters mentioned are from which book or link?
@Sachin - You can get the specification here: ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
0

May you have some javascript error code before this ?
It can break you browser interpretor and never run you try/catch code.

Comments

0

I've tested the above in both FF 3.5.9 and IE 8 and they behave as expected with an alert popping up. FF with the Firebug plugin turned on also shows the alert.

As others have stated, trying to create a new XMLHdfattpRequest is going to give you a ReferenceError in FF or a TypeError in IE which is definitely supposed to be caught by the catch block.

It seems then, that the problem lies with your browser, or your JavaScript interpreter. It may be bone-headed (I apologise for being so basic in my answer) but have you checked that JavaScript is actually enabled? After all, when you have eliminated the impossible, whatever remains, however improbable, must be the truth.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.