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,无论有无异常都会执行.
原文地址:https://www.cnblogs.com/hailexuexi/p/4672769.html