try 可以将容易出错的代码(对缓存的操作,http请求,文件的上传,下载)使用这个语句

允许测试代码块中的错误。

catch 捕捉错误

  1. <script>
  2. try{
  3. alertt("http")
  4. }catch(err){
  5. console.log(err)
  6. }
  7. console.log("hello world")
  8. /*ReferenceError: alertt is not defined
  9. at 01try-catch.html:11
  10. hello world
  11. */
  12. </script>

throw 可以自定义一个错误

<script>
        var arr = "";
        try{
            if(Array.isArray(arr)){
                console.log(err)
            }else{
                throw "必须传入一个数组"
            }
        }catch(err){
            console.log(err)
        }
</script>