1- try-catch

  1. try 可以将容易出错的代码(对缓存的操作,http操作,文件的上传,下载)
  2. 允许测试代码块中的错误
  3. catch 捕捉错误
  1. try{
  2. alertt("http")
  3. }catch(err){
  4. console.log(err)
  5. }
  6. console.log("hello world")
  7. /*
  8. ReferenceError: alertt is not defined
  9. hello world
  10. */

2- throw

可以自定义一个错误
var arr = ""
try{
  if(Array.isArray(arr)){
    console.log(arr.length)
  }else{
    throw "必须传入一个数组"
  }
}catch(err){
  console.log(err)
}

/*
 必须传入一个数组
*/