try{
//代码段
if(x>10) throw "Err1" //throw : 抛出错误,可以在catch中接到错误信息 else if(x<0) throw "Err2"
}
catch(er) {
//错误处理代码
if(er=="Err1") alert("Error! The value is too high") if(er == "Err2") alert("Error! The value is too low")
}
//示例:
<script type="text/javascript">
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>