源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

javascript中的try catch异常捕获机制用法分析

  • 时间:2021-02-04 02:33 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:javascript中的try catch异常捕获机制用法分析
本文实例讲述了javascript中的try catch异常捕获机制用法。分享给大家供大家参考,具体如下: 1.跟Java一样,JavaScript也具有try catch块,进行异常捕获的机制。 (1)典型的try catch语句
try{
}
catch{
}
finally{
}

跟java中一样,JS中最为典型的try catch语句也同样分为了三个部分,try用于捕获异常,catch用于处理异常,而finally用于关闭资源等后续操作。 举例:
try{
  throw "error"
}
catch(ex)
{
  console.log(ex);
}
finally{
  console.log("finally")
}

控制台依次输出:error,finally (2)try,catch块中,catch块和finally块只需要其一即可,因此如下的try catch块也是可以实现的 举例:
try {
  throw "error"
}
finally{
}

只抛出异常,其他情况不一一举例 (3)try catch块中包含了try catch块
try{ 
   try{
       throw "error"
     }
   finally{
      console.log("finally1")
     }
}
catch(ex)
{
   console.log(ex)
}
finally{
  console.log("finally2")
}

如果是像这样的嵌套循环,那么输出的顺序为:finally1,error,finally2 (4)嵌套try catch块中,抛出异常
try{
   try{
      throw "error1"
   }
   catch(ex)
   { 
      console.log(ex);
      throw "error2"
   }
   finally{
     console.log( "finally1")
   }
}
catch(ex)
{
    console.log(ex);
}
finally{
   console.log("finally2")
}

最终的输出为:error1,finally1,error2,finally2 更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/439.htm]JavaScript错误与调试技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/781.htm]JavaScript传值操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/751.htm]javascript编码操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/313.htm]JavaScript中json操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/502.htm]JavaScript切换特效与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/472.htm]JavaScript查找算法技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/462.htm]JavaScript动画特效与技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/297.htm]JavaScript数据结构与算法技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/281.htm]JavaScript遍历算法与技巧总结[/url]》及《[url=http://www.1sucai.cn/Special/119.htm]JavaScript数学运算用法总结[/url]》 希望本文所述对大家JavaScript程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部