1- try-catch
try 可以将容易出错的代码(对缓存的操作,http操作,文件的上传,下载)允许测试代码块中的错误catch 捕捉错误
try{alertt("http")}catch(err){console.log(err)}console.log("hello world")/*ReferenceError: alertt is not definedhello world*/
2- throw
可以自定义一个错误
var arr = ""
try{
if(Array.isArray(arr)){
console.log(arr.length)
}else{
throw "必须传入一个数组"
}
}catch(err){
console.log(err)
}
/*
必须传入一个数组
*/
