js中throw必须在try{}中吗? 如果一个throw不在try{}中该如何捕获呢?没搜到类似的例子,书商也没看到过。

2025年03月22日 05:55
有2个网友回答
网友(1):

throw不需要在try中,而随便一条语句就可以抛出个异常.try中可以捕获到你的代码中的throw
例如
function abc(){
var a = 1;
if(a == 1){
throw "a等于1";
}
}

try{
abc();
}catch(e){
alert(e);
}

网友(2):

我也没有。。知道 try cath throw