错误传播 --try{}catch(e){console.log(e)}
生活随笔
收集整理的这篇文章主要介绍了
错误传播 --try{}catch(e){console.log(e)}
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1、如果代码发生了错误,又没有被try ... catch捕获,那么,程序执行流程会跳转到哪呢?
function getLength(s) {return s.length; }function printLength() {console.log(getLength('abc')); // 3console.log(getLength(null)); // Error! }printLength();2、 如果在一个函数内部发生了错误,它自身没有捕获,错误就会被抛到外层调用函数,
如果外层函数也没有捕获,该错误会一直沿着函数调用链向上抛出,直到被JavaScript引擎捕获,代码终止执行。
所以,我们不必在每一个函数内部捕获错误,只需要在合适的地方来个统一捕获,一网打尽:
function main(s) {console.log('BEGIN main()');try {foo(s);} catch (e) {console.log('出错了:' + 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); BEGIN main() BEGIN foo() BEGIN bar() 出错了:TypeError: Cannot read property 'length' of null END main()
3、当bar()函数传入参数null时,代码会报错,错误会向上抛给调用方foo()函数,foo()函数没有try ... catch语句,所以错误继续向上抛给调用方main()函数,main()函数有try ... catch语句,所以错误最终在main()函数被处理了。
至于在哪些地方捕获错误比较合适,需要视情况而定。
转载于:https://www.cnblogs.com/slightFly/p/11576820.html
总结
以上是生活随笔为你收集整理的错误传播 --try{}catch(e){console.log(e)}的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: vue项目中使用echarts实现词云
- 下一篇: 异步错误处理 -- 时机