Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional catch clauses - browsers support

What browsers support Conditional catch clauses?

On MDN try...catch you can find Conditional catch clauses as Non-standard feature.

try {
    myroutine(); // may throw three exceptions
} catch (e if e instanceof TypeError) {
    // statements to handle TypeError exceptions
} catch (e if e instanceof RangeError) {
    // statements to handle RangeError exceptions
} catch (e if e instanceof EvalError) {
    // statements to handle EvalError exceptions
} catch (e) {
    // statements to handle any unspecified exceptions
    logMyErrors(e); // pass exception object to error handler
}

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

It's supported by any modern browsers?

Google Chrome's console returned Uncaught SyntaxError: Unexpected token if

Or shoud I use:

try {
    myroutine(); // may throw three exceptions
} catch (e) {
    if(e instanceof TypeError) {
        // statements to handle TypeError exceptions
    }
    else if(e instanceof RangeError) {
        // statements to handle RangeError exceptions
    }
    else if(e instanceof EvalError) {
        // statements to handle EvalError exceptions
    }
    else {
        // statements to handle any unspecified exceptions
        logMyErrors(e); // pass exception object to error handler
    }
}
like image 991
Shaddow Avatar asked Dec 10 '13 00:12

Shaddow


People also ask

Does try catch stop execution JavaScript?

The “try… First, the code in try {...} is executed. If there were no errors, then catch (err) is ignored: the execution reaches the end of try and goes on, skipping catch . If an error occurs, then the try execution is stopped, and control flows to the beginning of catch (err) .

Can we use nested try catch in JavaScript?

You can nest one or more try statements. If an inner try statement does not have a catch -block, the enclosing try statement's catch -block is used instead. You can also use the try statement to handle JavaScript exceptions. See the JavaScript Guide for more information on JavaScript exceptions.

Can we use catch () without passing arguments in it?

Some exception can not be catch(Exception) catched. Below excecption in mono on linux, should catch without parameter. Otherwise runtime will ignore catch(Exception) statment.

How do you handle errors in catch block?

You can put a try catch inside the catch block, or you can simply throw the exception again. Its better to have finally block with your try catch so that even if an exception occurs in the catch block, finally block code gets executed. Finally block may not get executed in certain exceptions.


Video Answer


2 Answers

You should use the later one, as it works everywhere, quoting MDN

And here is how to do implement the same "Conditional catch clauses" using only simple JavaScript conforming to the ECMAScript specification (obviously it's more verbose, but works everywhere (I have tested on Firefox, IE and Chrome)), I have :

try {
    myroutine(); // may throw three types of exceptions
} catch (e) {
    if (e instanceof TypeError) {
        // statements to handle TypeError exceptions
    } else if (e instanceof RangeError) {
        // statements to handle RangeError exceptions
    } else if (e instanceof EvalError) {
        // statements to handle EvalError exceptions
    } else {
       // statements to handle any unspecified exceptions
       logMyErrors(e); // pass exception object to error handler
    }
}

The first one catch (e if e instanceof ..) only works with Mozilla Firefox and gives errors on chrome and IE

Chrome

Uncaught SyntaxError: Unexpected token if

IE

SCRIPT1006: Expected ')'
like image 100
Anil Avatar answered Sep 28 '22 00:09

Anil


On MDN try...catch you can find Conditional catch clauses as Non-standard feature.  

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

What this means is that this is not part of the javascript language that all browsers and stuff have agreed upon having available. Which most certainly will mean that not all browsers will support it if any. Your best option regarding a cross-browser solution would be the second case using a switch clause. GL

like image 26
php_nub_qq Avatar answered Sep 28 '22 01:09

php_nub_qq