|
2.再试试语法错误呢?
- window.onerror = function(message, source, lineno, colno, error) {
- console.log('捕获到异常:',{message, source, lineno, colno, error});
- }
- let name = 'Jartto
控制台打印出了这样的异常:
- Uncaught SyntaxError: Invalid or unexpected token
什么,竟然没有捕获到语法错误?
3.怀着忐忑的心,我们最后来试试异步运行时错误:
- window.onerror = function(message, source, lineno, colno, error) {
- console.log('捕获到异常:',{message, source, lineno, colno, error});
- }
- setTimeout(() => {
- Jartto;
- });
控制台输出了:
- 捕获到异常: {message: "Uncaught ReferenceError: Jartto is not defined", source: "http://127.0.0.1:8001/", lineno: 36, colno: 5, error: ReferenceError: Jartto is not defined
- at setTimeout (http://127.0.0.1:8001/:36:5)}
4.接着,我们试试网络请求异常的情况:
- <script>
- window.onerror = function(message, source, lineno, colno, error) {
- console.log('捕获到异常:',{message, source, lineno, colno, error});
- return true;
- }
- </script>
- <img src="./jartto.png">
我们发现,不论是静态资源异常,,或者接口异常,错误都无法捕获到。
补充一点:window.onerror 函数只有在返回 true 的时候,异常才不会向上抛出,否则即使是知道异常的发生控制台还是会显示 Uncaught Error: xxxxx
- window.onerror = function(message, source, lineno, colno, error) {
- console.log('捕获到异常:',{message, source, lineno, colno, error});
- return true;
- }
- setTimeout(() => {
- Jartto;
- });
(编辑:淮安站长网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|