try 语句使您能够测试代码块中的错误。
catch 语句允许您处理错误。
throw 语句允许您创建自定义错误。
finally 使您能够执行代码,在 try 和 catch 之后,无论结果如何。
1.try-catch
try{alertt(1)}catch(err){console.log(err.message)}//alertt is not defined
2.throw
throw 语句允许您创建自定义错误。
从技术上讲您能够抛出异常(抛出错误)。
var arr ="jfd";try{if(Array.isArray(arr)){console.log(arr.length)}else{throw new Error("必须是数组")}}catch(err){console.log(err)}
3.finally
var x = 1;try{if(x==1) throw "x不能等于1"}catch(err){console.log(err)}finally{console.log("捕获异常结束")}
