若是代码发生了错误,又没有被try ... catch捕获,那么,程序执行流程会跳转到哪呢?函数
function getLength(s) { return s.length; } function printLength() { console.log(getLength('abc')); // 3 console.log(getLength(null)); // Error! } printLength();
若是在一个函数内部发生了错误,它自身没有捕获,错误就会被抛到外层调用函数,若是外层函数也没有捕获,该错误会一直沿着函数调用链向上抛出,直到被JavaScript引擎捕获,代码终止执行。spa
因此,咱们没必要在每个函数内部捕获错误,只须要在合适的地方来个统一捕获,一网打尽:code
'use strict'; function main(s) { console.log('BEGIN main()'); try { foo(s); } catch (e) { alert('出错了:' + e); } console.log('END main()'); } function foo(s) { console.log('BEGIN foo()'); bar(s); console.log('END foo()'); } function bar(s) { console.log('BEGIN bar()'); console.log('length = ' + s.length); console.log('END bar()'); } main(null);
当bar()
函数传入参数null
时,代码会报错,错误会向上抛给调用方foo()
函数,foo()
函数没有try ... catch语句,因此错误继续向上抛给调用方main()
函数,main()
函数有try ... catch语句,因此错误最终在main()
函数被处理了。blog
至于在哪些地方捕获错误比较合适,须要视状况而定。ip