手动抛异常
function funa() {
let a = 2;
if (a !== 1) {
throw new Error("报错:a!==1"); //手动抛异常
}
}
捕获异常
try {
funa();
} catch (err) {
console.log("err:", err);
//Error: 报错:a!==1 at funa (error.js:4:11)...
console.log("err.message:", err.message); // 报错:a!==1
}
console.log("end");
网友评论