代码示例:(标识:eg_js_try_catch_2)
<!DOCTYPE html>
<html>
<head>
<script>
var txt = "";
function message() {
  try {
    adddlert("Welcome guest!");
  }
  catch(err) {
    txt = "此页面出错。\n\n";
    txt += "单击“确定”继续查看此页面,\n";
    txt += "或取消以返回主页。\n\n";
    if(!confirm(txt)) {
      document.location.href = "http://www.sou-xun.com/";
    }
  }
}
</script>
</head>

<body>
<input type="button" value="查看消息" onclick="message()" />
</body>

</html>
运行结果: