JavaScript错误处理

一、onerror事件

<script type="text/javascript">
onerror=function()

{}

 

onerror=handleErr
var txt=""

function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + msg + "\n"
txt+="URL: " + url + "\n"
txt+="Line: " + l + "\n\n"
txt+="Click OK to continue.\n\n"
alert(txt)
return true
}

function message()
{
adddlert("Welcome guest!")
}
</script>

二、try catch

<script type="text/javascript">
var txt=""
function message()
{
try
  {
  adddlert("Welcome guest!")

//throw "fdsf";

  }
catch(err)
  {
  txt="此页面存在一个错误。\n\n"
  txt+="错误描述: " + err.description + "\n\n"
  txt+="点击OK继续。\n\n"
  alert(txt)
  }
}
</script>

posted @ 2011-11-25 10:59    阅读(187)  评论(0编辑  收藏  举报