在 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/