try{}catch{}

当里面有错误时不抛出错误,而且运行catch里面的语句,try里面错误语句的后续代码不再运行,但是不影响后续代码运行
 
try {
    alertt("111111111")
} catch(error) { // 打印出错误类型和错误信息
    console.log(error.name + " :" + error.message); // ReferenceError: alertt is not defined
    // error.name: ReferenceError
    // error.message: alertt is not defined
} finally { // catch 和 finally 语句都是可选的,但你在使用 try 语句时必须至少使用一个
  console.log("无论成功失败都会执行")
}
console.log('不影响本条console.log运行');

 

let str = "";
try {
    // throw: 语句抛出一个自定义错误
    // 抛出类型: 异常可以是 JavaScript 字符串、数字、逻辑值或对象
    if(str === "") throw "当前str是空";
} catch(error) { // 打印出错误类型和错误信息
    console.log(error); // error = 当前str是空
}

 

六种错误类型:
1.EvalError: eval()的使用与定义不一致
2.RangeError: 数值越界
3.ReferenceError: 非法或者不能识别的引用数值
4.SyntaxError: 发生语法解析错误
5.TypeError: 操作数类型错误
6.URIError: URL处理函数使用不当
 
posted @ 2021-05-11 15:03  钱途似锦  阅读(93)  评论(0)    收藏  举报