error handling - Why don't JavaScript libraries use try-catch blocks more often? -
i'm still relatively new javascript, coming more classical (i.e. java, actionscript 3.0) background. i'm finding it's common incorrect implementation of library/framework's api break things further call stack, without clear indication it's application code (not library code) breaking things.
for example, jquery.trigger() call may invoke handler throws error, , invocation not wrapped in try-catch (nor implements other kind of error protection), , prevents other handlers firing.
i understand error should halt execution, seems library code better sandboxed application code, , see kind of breakage more in js libs in other languages i've worked with.
firstly, because it's pain catch single exception:
try { dosomething(); } catch(e) { if (e instanceof someexception) { // handle someexception } else { throw e; // , lose stacktrace information :-( } }
and catching all exceptions wrong thing.
secondly, because exception hierarchy of javascript poor in differentiating different kinds of error , providing informational properties them. javascript prefers might have meant , quietly break when wasn't, raise error (see undefined
et al). historically inconsistent between browsers (especially dom exceptions). means there no culture of using exceptions , inherits library design.
Comments
Post a Comment