例一:
function
message(){   try   {     adddlert("Welcome guest!")   }   catch(err)   {     txt="此页面存在一个错误。\n\n"      txt+="错误描述: " + err.description + "\n\n"      txt+="点击OK继续。\n\n"     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,无论有无异常都会执行.

 

相关文章:

  • 2023-02-16
  • 2022-01-31
  • 2021-12-04
  • 2021-10-19
  • 2022-02-21
  • 2022-01-18
  • 2021-07-14
猜你喜欢
  • 2021-12-24
  • 2022-01-21
  • 2021-10-27
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案