Jquery中try catch finally的使用



01例一:
02function message(){
03  try
04    {
05      adddlert("Welcome guest!")
06    }
07  catch(err)
08    {
09      txt="此页面存在一个错误。\n\n"
10      txt ="错误描述: "   err.description   "\n\n"
11      txt ="点击OK继续。\n\n"
12      alert(txt)
13    }
14}
01例二:
02var array = null;
03try {
04    document.write(array[0]);
05} catch(err) {
06    document.writeln("Error name: "   err.name   "");
07    document.writeln("Error message: "   err.message);
08}
09finally{
10    alert("object is null");
11}




程序执行过程

1. array[0]的时候由于没有创建array数组,array是个空对象,程序中调用array[0]就会产生object is null的异常 
2. catch(err)语句捕获到这个异常通过err.name打印了错误类型,err.message打印了错误的详细信息. 
3. finally类似于java的finally,无论有无异常都会执行.


原文链接:Jquery中try catch finally的使用