js的try{}catch(e){}与报错

//在try里面发生的错误,不会执行错误后面的代码
try{
	console.log('a'); //a
	console.log(b);
	console.log('c');
}catch(e){//捕捉错误,错误就不会显示在控制台,但如果没有错误就不会执行catch语句
	//e <---> error  -->error.name、error.message
	console.log(e); //ReferenceError: b is not defined
	console.log(e.name); //ReferenceError
	console.log(e.message); // b is not defined
}
console.log('d'); //d

Error.name六种错误:
1、EvalError: eval()的使用定义不一致,eval()已经不使用了
2、RangeError: 数值越界
3、ReferenceError:非法或不能识别的引用数值(*),当变量未经声明就使用时会报这个错误
4、SyntaxError:发生语法解析错误(*)
5、TypeError:操作数类型错误
6、URLError:URL处理函数使用不当

例:

var obj = a; //报错ReferenceError

var obj = {a: b;} //报错 SyntaxError

猜你喜欢

转载自blog.csdn.net/LLL_liuhui/article/details/86287089
今日推荐