javascript - 为什么抛出异常后就不再认为发生了?

标签 javascript exception ecmascript-5

5.2 Algorithm Conventions ECMAScript® 语言规范 5.1 版:

If an algorithm is defined to “throw an exception”, execution of the algorithm is terminated and no result is returned. The calling algorithms are also terminated, until an algorithm step is reached that explicitly deals with the exception, using terminology such as “If an exception was thrown…”. Once such an algorithm step has been encountered the exception is no longer considered to have occurred.

为什么抛出异常后就不再认为发生了?我不明白。有一个算法步骤“处理”异常,因此我认为应该将其视为“发生”。

(提问前我想先上网搜索一下,但不知道该搜索什么。)

最佳答案

可以用这个示例代码来解释:

// our algorithm
function foo()
{
    // throw the exception
    throw 'a ball';
    console.log('this is not output'); // terminated statement
}

// the calling algorithm
function bar()
{
    try {
        foo();
    } catch (e) {
        // dealing with the exception
    }
    // no longer an exception here
    console.log('all is fine');
}

关于javascript - 为什么抛出异常后就不再认为发生了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27180166/

相关文章:

javascript - 淡入带有文本的 div

javascript - 当 2 个或更多相同时,是否可以取消绑定(bind) jQuery 中的特定事件处理程序?

c++ - 析构函数中的访问冲突 'deleting' 指针

c# - 如何处理任务等待程序中未处理的异常

javascript - Javascript 中的 Object.__proto__.__proto__ 对象是否有通用术语/名称?

javascript - 如何从 JSON 数组中检索 JSON 数据?

javascript - 如何在 JavaScript 中打开和关闭灯泡?

vb.net - 如何在 VB.NET 中引发异常

javascript - 如何在 TypeScript/Javascript 中递归迭代对象数组

javascript - 如何在不选择模式配置参数的情况下使用 Mongoose 在 MongoDB 模式实例化的关联数组/对象中执行 foreach?