jslint and conditional catch blocks

S

Simon Oberhammer

not sure if this is rhino specific, but in accordance to the mozilla-
docs [1] we have a lot of the following in our code. and jslint
doesn't like it:

try {
// function could throw exceptions
} catch (e if e == "InvalidNameException") {

} catch (e....)

jslint says:

line 50 Expected ')' and instead saw 'if'.
} catch (e if e instanceof Exception) {

line 50 Expected '{' and instead saw 'e'.
} catch (e if e instanceof Exception) {

line 50 Expected an assignment or function call and instead saw an
expression.
} catch (e if e instanceof Exception) {


not sure if i should fix jslint or our code...

greetings
simon
 
H

Henry

not sure if this is rhino specific, but in accordance to
the mozilla- docs [1] we have a lot of the following in
our code. and jslint doesn't like it:

try {
// function could throw exceptions

} catch (e if e == "InvalidNameException") {

This is a JavaScript(tm) extension, conditional catch is not part of
ECMA 262 (and will not even be part of the next (3.1) version, which
is a pity as without them the try-catch system is javascript is not
particularly useful.

not sure if i should fix jslint or our code...

Jslint is checking against ECMA 262 (3rd Ed.) and your code is outside
of ECMAScript, so neither are wrong as such, just incompatible.
 
M

Martin Rinehart

try {
// function could throw exceptions

} catch (e if e == "InvalidNameException") {
} catch (e....)

Conditional catch is a compile error, Opera on KDE.
 
T

Thomas 'PointedEars' Lahn

Simon said:
not sure if this is rhino specific, but in accordance to the mozilla-
docs [1] we have a lot of the following in our code. and jslint
doesn't like it:

try {
// function could throw exceptions
} catch (e if e == "InvalidNameException") {

That is either not the code that you used, or the messages that you posted
below are not the ones that you received, or you posted only the irrelevant
parts of your code. Either way, please make it better next time.

In general, this code doesn't make much sense anyway. The string
representation of an Error object is specified to be implementation defined,
and if `e' is a user-defined object instead, its string representation would
have to be `InvalidNameException' for this to work. Probably you were
looking for

try
{
// code that can throw a user-defined InvalidNameException object
}
catch (e if e instanceof InvalidNameException)
{
// ...
}

instead.
} catch (e....)

jslint says:

line 50 Expected ')' and instead saw 'if'.
} catch (e if e instanceof Exception) {

jslint does not seem to know about proprietary extensions to the ECMAScript
Language Specification such as this feature, supported only in Mozilla.org
JavaScript[tm](R) since version 1.5 (since Firefox 1.0 and friends).

<http://PointedEars.de/es-matrix/#try>

I would consider this a(nother) bug in jslint since there can be no doubt
about what Rhino is, and jslint has an explicit "Rhino option" that does
not help here when it should (see below).
not sure if i should fix jslint or our code...

Since the code is supposed to run by Rhino, which is the Java implementation
of the JavaScript[tm](R) script engine (as compared to SpiderMonkey, the C
implementation of that engine, which is used in many Mozilla.org end-user
software, including Firefox), you can leave the code as it is.

However, I recommend you use the `instanceof' operator in favor of `==' in
all `catch' statements.

Sounds like a good idea if you fixed jslint so that it worked properly with
Rhino and sent Douglas Crockford a patch (if you wanted to). But he could
be faster than you ;-)


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top