- Anybody can ask a question
- Anybody can answer
- The best answers are voted up and rise to the top
One should avoid
Recommendations in the node.js community is that you pass errors around in callbacks (Because errors only occur for asynchronous operations) as the first argument
There are also other issues like try / catch is really expensive and it's ugly and it simply doesn't work with asynchronous operations.
So since synchronous operations should not throw an error and it doesn't work with asynchronous operations, no-one uses try catch except for errors thrown by host objects or ECMAScript
The problem is that the control flow leaves the
Regardless, try-catch should be used, but of course you should learn how to use them properly - like everything else in programming.
A lot of these answers are a bit old and do not take into account the new ES7 features
I believe that much of the reason that
Some of the major issues in other languages that cause exceptions to occur simply don't exist in JS. Type casting isn't needed the vast majority of the time. Instead, the preferred method is typically to feature check (enforcing a particular interface):
As a result, some older browsers don't support it. (In fact, it may cause a parser/syntax error in some older browsers, something that's more difficult to "program defensively" against than most other types of errors.)