1、try-catch

普通报错无法接着执行之后语句

  1. <script>
  2. con.log("hello world")
  3. console.log(2);
  4. </script>

image.png
try-catch语句下不影响接下里的语句执行

  1. <script>
  2. try{
  3. con.log("hello world")
  4. }catch(err){
  5. console.log(err);
  6. /* 抛出一个错误 */
  7. //throw new Error("错误")
  8. }
  9. console.log(2);
  10. </script>

image.png

  1. /* 抛出一个错误 */
  2. throw new Error("错误")

2、finally

finally不管有没有异常,都会执行

  1. <!--
  2. try-catch-finally
  3. finally不管有没有异常,都会执行
  4. -->
  5. <script>
  6. try{
  7. console.log(1);
  8. con.log("hello world")
  9. }catch(err){
  10. console.log(err);
  11. }finally{
  12. console.log(2);
  13. }
  14. </script>

image.png