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);
  }
Gabe
  • 49,577
  • 28
  • 142
  • 181
steve
  • 1
  • 1
  • 10
    in what browser? – meder omuraliev Nov 17 '10 at 17:38
  • @steve Could be that an earlier error is thrown so that the above code is not executed. – Šime Vidas Nov 17 '10 at 17:41
  • 1
    It's getting caught in FF testing using FireBug. – wajiw Nov 17 '10 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)? – Mike Webb Nov 17 '10 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. – Šime Vidas Nov 17 '10 at 17:57
  • @Šime Vidas - Ok. Wasn't sure. Sounds like it actually gets caught, so this must not be one of those cases. I know that sometimes it does happen where code is just ignored, craps out, and the page runs as if nothing happened. It winds up being sort of a pain to debug at that point, but its a whole different issue. – Mike Webb Nov 17 '10 at 19:39
  • @Mike I think I know what you are talking about. You see, each SCRIPT element on a given web-page is treated as a separate program. If one SCRIPT element (the code in it) throws an error, that particular program will break, but all other SCRIPT elements will execute normally. I wrote a question about this behavior: http://stackoverflow.com/questions/3735406/how-many-javascript-programs-are-executed-for-single-web-page-in-the-browser – Šime Vidas Nov 17 '10 at 22:20
  • @Šime Vidas - That is exactly what I was thinking of. – Mike Webb Nov 17 '10 at 22:55

3 Answers3

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.

ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
  • @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 Nov 17 '10 at 18:03
  • @Šime Vidas - You need to dig a bit deeper. Read: **10.2.1.2** -> **11.2.2** -> **8.7.1** – ChaosPandion Nov 17 '10 at 18:19
  • @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. – Šime Vidas Nov 17 '10 at 18:41
  • @Chaos, @Sime Vidas - The chapters mentioned are from which book or link? – Sachin Shanbhag Nov 17 '10 at 20:03
  • @Sachin - You can get the specification here: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf – ChaosPandion Nov 17 '10 at 21:15
0

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

diewland
  • 1,865
  • 5
  • 30
  • 41
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.

Gary
  • 7,167
  • 3
  • 38
  • 57