gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 21:54:29 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com