异常捕获
1、异常
当JavaScript引擎执行Javascript代码时,发生了错误,导致程序停止运行
2、异常抛出
当异常产生,并且将这个异常生成一个错误信息
3、异常捕获
try{
发生异常的代码块;
}catch(err){
错误信息处理;
}
function demo(){try{alert(str);}catch(error){console.log(error);//==>ReferenceError: str is not defined(…)}}demo(); 自定义错误: function demo(){try{ if(str==""){ throw "str为空"; }}catch(error){console.log(error);//==>str为空}}demo(); |