使用严格模式

时间:2017-11-27 11:07:50

标签: javascript exception exception-handling try-catch

我正在使用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 questionconst的范围有关,而不是try/catch本身的行为。

0 个答案:

没有答案