我正在使用use strict
模式声明的现有函数。我发现奇怪的是,当抛出异常时,catch
块不会被执行。我认为它与strict mode
行有关,因为当我乘坐它时,它会进入catch
块。
我总是看到使用try/catch
的主要目的是防止在运行时出现无法控制的情况时突然退出代码(没有任何控制)。
catch 子句包含指定如果的操作的语句 在try块中抛出异常。
使用严格模式时,似乎没有任何效果。对这种行为有什么解释吗?
我的代码:
function test(){
"use strict"
//...
try {
// var dt = new Date(2017, 10, 27, 15, 23, 9); /* works fine */
var dt = new Date(2017, 10, 27, 15, 23, 09);
} catch(e){
console.log("inside catch");
} finally {
console.log("inside finally");
}
//...
}
test();
// output: Uncaught SyntaxError:
// Decimals with leading zeros are not allowed in strict mode.
备注
在提出问题之前,我在StackOverflow上查看了一些问题,但我没有找到解释。例如,this question与const
的范围有关,而不是try/catch
本身的行为。