Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript catch parameter already defined
    text
    copied!<p>I'm trying to understand <em>why</em> I'm getting the following error, not <em>how</em> to work around it.</p> <p>Passing the following code to <a href="http://jslint.com/" rel="noreferrer" title="The JavaScript Code Quality Tool by Douglas Crockford">JSLint</a> or <a href="http://jshint.com/" rel="noreferrer" title="A fork of JSLint designed to be more flexible.">JSHint</a> yields the error <strong>'err' is already defined.</strong></p> <pre><code>/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: true, windows: true, bitwise: true, newcap: true, strict: true, maxerr: 50, indent: 4 */ function xyzzy() { "use strict"; try { /*Step 1*/ } catch (err) { } try { /*Step 2*/ } catch (err) { } } </code></pre> <p>The obvious assumption here is that <code>catch</code> behaves, or should behave, like a function. Thus, <code>err</code> is neither a global variable, nor a local variable to <code>xyzzy</code>, but a <em>parameter</em> for the <code>catch</code> block.</p> <p>In browsing the <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm" rel="noreferrer" title="This Standard defines the ECMAScript scripting language.">ECMA-262 Standard</a>, section 12.14 describing <em>The <code>try</code> Statement</em> indicates that the <code>catch</code> clause takes an <em>Identifier</em> that is <em>bound</em> to an exception. Additionally the semantic production rule for <code>catch</code> refers to a <em>parameter</em> that's passed calling out <em>Identifier</em> as an <em>argument</em>.</p> <p>This seems to suggest to the casual reader that the above code <em>is valid</em> and that perhaps the lint tools have a bug.</p> <p>Even <a href="http://www.jetbrains.com/idea/" rel="noreferrer" title="IntelliJ IDEA (you need the Ultimate Edition to do this)">IntelliJ</a>'s strictest JavaScript code inspection analysis doesn't report there being a problem with <code>err</code> being redefined.</p> <p>More of a concern, if it is a variable scoping concern, then one might surmise that the <code>err</code> is bleeding into the global space, which poses a whole host of other problems, and that instead one should declare it up front, like this:</p> <pre><code>/*jslint white: true, devel: true, onevar: true, browser: true, undef: true, nomen: true, regexp: true, plusplus: true, windows: true, bitwise: true, newcap: true, strict: true, maxerr: 50, indent: 4 */ function xyzzy() { "use strict"; var err; // DECLARE err SO IT IS CERTAINLY LOCAL try { /*Step 1*/ } catch (err) { } try { /*Step 2*/ } catch (err) { } } </code></pre> <p>But this only results now in two errors about <code>err</code> at each of the catch statements, making the problem worse and potentially introducing <a href="http://en.wikipedia.org/wiki/Variable_shadowing" rel="noreferrer" title="The catch&#39;s err hides the outer scope&#39;s local variable, also called err.">variable shadowing</a>.</p> <p>The lint tools are suggesting that each <code>catch</code> block introduces not just it's own lexical scope, but a new variable as well. This can't be right.</p> <p>Simply making <code>err1</code>, <code>err2</code>, ... to placate the static analysis tools merely hides the symptom and doesn't contribute to cleaner code.</p> <p><strong>JavaScript Gurus</strong>: Is this a bug in the lint tool, a dark corner with the JavaScript spec, or a fundamental misunderstanding of what's happening here?</p> <p>UPDATE: <em>Wrote to Douglas Crockford, author of JSLint, and it turns out there's a very valid reason for this warning. See answer below.</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload