try{//Run some code here}catch(err){//Handle errors here}
try{
if(a==1)alert("var a is 3");
}catch(e){
alert(e);
}
//由于变量a没有定义就拿来与3进行比较了, 所以报错, 这时就用catch(e)来捕获错误, 并用alert(e)进行处理(显示错误信息).
//catch中的e变量是exception的缩写,有时也用ex,习惯问题,实际用什么都行。
try{//Run some code here}catch(err){//Handle errors here}
try{
if(a==1)alert("var a is 3");
}catch(e){
alert(e);
}
//由于变量a没有定义就拿来与3进行比较了, 所以报错, 这时就用catch(e)来捕获错误, 并用alert(e)进行处理(显示错误信息).
//catch中的e变量是exception的缩写,有时也用ex,习惯问题,实际用什么都行。