8

The javascript, when run through JSLint yells at me and I am not sure why.

/*jslint browser: true, devel: true, evil: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, newcap: true, immed: true */

var foo = function() {
  try {
    console.log('foo');
  } catch(e) {
    alert(e);
  }
  
  try {
    console.log('bar');
  } catch(e) {
    alert(e);
  }
};

foo();

It tells me:

Problem at line 12 character 11: 'e' is already defined.

} catch(e) {

It appears to be upset that I have a second catch(e). Why would this be an issue? Does it not simply set e to local variable inside the catch block? Do I need to uniquely name the local variables for all trapped errors in a function?

Community
  • 1
  • 1
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337

5 Answers5

9

To JSLint, try..catch has the implicit effect of declaring e as a local variable. Because you have two such blocks within the same function (there is no block scope in JavaScript), JSLint sees that as declaring a variable that has already been declared.

Naming the variables e1, e2, etc. would prevent this warning from JSLint. Is it really a problem though? The ECMAScript 5 specification, section 12.14, says "No matter how control leaves the Block the LexicalEnvironment is always restored to its former state." This, in fact, does appear to be the case:

try {
    throw new Error("testing 1234");
} catch(fooBarBaz){
    alert("Catch: " + fooBarBaz);    // works
}

alert(fooBarBaz);    // throws exception

So, to conclude, this is simply a limitation of JSLint and is unlikely to lead to any practical problem.

PleaseStand
  • 31,641
  • 6
  • 68
  • 95
  • It appears you are correct. I guess that try/catch does _not_ introduce scope, as proved by this quick test I put up: http://jsfiddle.net/VRcwV/ – Alex Wayne Nov 17 '10 at 22:05
  • Oh just saw the edit! So it does not introduce scope, but the local variable created by the catch statement is not available outside that catch. So it does introduce a tiny bit of specialized scope just for that exception? – Alex Wayne Nov 17 '10 at 22:09
  • 1
    @Squeegy: IE 8 is *not* compliant, it seems. Try my test (http://jsfiddle.net/DpHMt/) in that browser, and you will see *both* alert boxes open. – PleaseStand Nov 17 '10 at 22:22
0

The JSLint I use shows no error at all - and logical your code is correct.

pex
  • 7,351
  • 4
  • 32
  • 41
0

Try to use a different variable, maybe its getting confused because e is usually reserved for event handlers.

Darren
  • 10,631
  • 8
  • 42
  • 64
0

JSLint might simply be wrong here. According to the ECMAScript spec, entering a catch block creates a new scope inside which the exception variable is defined. In your example, e is valid only inside the catch block and is not defined outside. There is no redefinition here.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • 1
    ECMAScipt spec, sure. But browser implementation, apparently not: http://jsfiddle.net/VRcwV/ – Alex Wayne Nov 17 '10 at 22:03
  • @Squeegy: It's only the exception variable `e` that goes into the new scope. Variables declared with `var` are always at function scope. Try `alert(e)` and you'll see that it's undefined outside the `catch` block. – casablanca Nov 17 '10 at 22:09
-1

Use a different variable for each try / catch.

RussellUresti
  • 6,211
  • 4
  • 28
  • 26