Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use catch(e if e instanceof SyntaxError) in Node.js's javascript?

I read about try catch(e if e instanceof ...) blocks on MDN, however, upon trying it in Node.js, I get a SyntaxError: Unexpected token if.

If this doesn't work, is there another way to catch specific exceptions, instead of everything that might occur?

like image 878
lowerkey Avatar asked May 07 '12 17:05

lowerkey


1 Answers

To cite the MDN doc you linked to:

Note: This functionality is not part of the ECMAScript specification.

and

JavaScript 1.5, NES 6.0: Added multiple catch clauses (Netscape extension).

so: No, this won't be possible in Node.js. Yet, of course you can use the following syntax as a workaround:

try {
    try_statements
} catch (e) {
    if (e instanceof ...)
        catch_statements_1
    else if (e instanceof ...)
        catch_statements_2
    else
        throw e;
} [finally {
    finally_statements
}]
like image 68
Bergi Avatar answered Sep 28 '22 02:09

Bergi