• javascript中 try catch finally 的使用


    例一:
    function
    message(){   try   {     adddlert("Welcome guest!")   }   catch(err)   {     txt="此页面存在一个错误。 "      txt+="错误描述: " + err.description + " "      txt+="点击OK继续。 "     alert(txt)   } }
    例二:
    var array = null;
    try {
        document.write(array[0]);
    } catch(err) {
        document.writeln("Error name: " + err.name + "");
        document.writeln("Error message: " + err.message);
    }
    finally{
        alert("object is null");
    }
    
    程序执行过程
    
    1. array[0]的时候由于没有创建array数组,array是个空对象,程序中调用array[0]就会产生object is null的异常 
    2. catch(err)语句捕获到这个异常通过err.name打印了错误类型,err.message打印了错误的详细信息. 
    3. finally类似于java的finally,无论有无异常都会执行.
  • 相关阅读:
    在centos上搭建Git服务器
    glog日志库移植Android平台
    水葱
    路易斯安娜鸢尾
    再力花
    矮生百慕大
    洒金珊瑚
    八角金盘
    锦绣杜鹃
    茶梅球
  • 原文地址:https://www.cnblogs.com/hailexuexi/p/4672769.html
Copyright © 2020-2023  润新知