Javascript中是否存在无法捕获的异常? [英] Do uncatchable exceptions exist in Javascript?

查看:124
本文介绍了Javascript中是否存在无法捕获的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何javascript运行时(浏览器,节点等)是否会抛出无法捕获的异常?是否在javascript环境中遇到的任何和所有异常都可以在try / catch语句中捕获?

Do any javascript runtimes (browsers, Node, etc.) ever throw uncatchable exceptions? Are any and all exceptions ever encountered in a javascript environment catchable in a try/catch statement?

推荐答案

如果通过例外你的意思是任何破坏你的脚本的异常条件,然后所有这些都会抛出无法捕获的异常,因为大多数语法错误都不可捕获。只能捕获动态评估代码( eval new Function )的语法错误。

If by exceptions you mean any exceptional condition that breaks your script, then all of them can throw uncatchable exceptions, since most syntax errors aren't catchable. Only syntax errors from dynamically evaluated code (eval, new Function) can be caught.

try { :( } catch(e) { } // uncatchable syntax error

假设你的意思是使用 try..catch 进行捕获。技术上你可以使用错误从其他脚本块中捕获语法错误的事件:

That's assuming you mean catchable using try..catch. Technically you could use the error event to trap syntax errors from other script blocks:

<script> onerror = function (e) { return true; }; </script>
<script> :( </script>






另一方面,也许你不认为在评估之前发生的错误是异常。在这种情况下,无法捕获的异常可能会降级为其他异常抛出的异常执行上下文(例如用setTimeout调用的函数),你无法控制抛出异常的执行上下文。当然,这些异常不会破坏程序的流程。


On the other hand, maybe you don't consider errors that happen before evaluation to be exceptions. In that case "uncatchable exceptions" may be relegated to exceptions throws from other execution contexts (such as a function invoked with setTimeout), where you don't have control over the execution context throwing the exception. Of course, these exceptions will not disrupt the flow of your program.

这篇关于Javascript中是否存在无法捕获的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆