javascript查缺补漏之一 ——《流程控制与错误处理》
/ / 点击 / 阅读耗时 2 分钟如果finally块返回一个值,该值会是整个try-catch-finally流程的返回值,不管在try和catch块中语句返回了什么。
下面代码中有说明,catch中的return会挂起,直到finally中的代码完成,finally中的return会覆盖catch中的return。return之后的console都不会执行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function f() {
try {
console.log(0);
throw "bogus";
} catch(e) {
console.log(1);
return true; // this return statement is suspended
// until finally block has completed
console.log(2); // not reachable
} finally {
console.log(3);
return false; // overwrites the previous "return"
console.log(4); // not reachable
}
// "return false" is executed now
console.log(5); // not reachable
}
f(); // console 0, 1, 3; returns false
catch中可以再次抛出异常,但如果finally中有返回值,则这个返回值会覆盖catch中抛出的异常。
1
2
3
4
5
6
7
8
9
10
11
12function f() {
try {
throw 'bogus';
} catch(e) {
console.log('caught inner "bogus"');
throw e; // this throw statement is suspended until
// finally block has completed
} finally {
return false; // overwrites the previous "throw"
}
// "return false" is executed now
}嵌套try…catch
任何给定的异常只会被离它最近的封闭 catch 块捕获一次。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20try {
try {
throw new Error("oops");
}
catch (ex) {
console.error("inner", ex.message);
throw ex;
}
finally {
console.log("finally");
}
}
catch (ex) {
console.error("outer", ex.message);
}
// Output:
// "inner" "oops"
// "finally"
// "outer" "oops"
全文完。