2013年8月3日 星期六

javascript try throw error exception

<html>
<body>
<script type="text/javascript">
var x=prompt("½Ð¿é¤J0¨ì10¤§¶¡ªº¤@­Ó¼Æ¦r:","")
try

if(x>10) 
throw "Err1"
else if(x<0)
throw "Err2"

catch(er)
{
if(er=="Err1") 
alert("¿ù»~¡I ­È¤Ó¤j¤F")
if(er == "Err2") 
alert("¿ù»~¡I ­È¤Ó¤p¤F") 
}
</script>
</body>
</html>



<!----------------------------------- try catch ------------------------------------------->

 <html>
<head>
<script type="text/javascript">
var txt=""
function message()
{
try
  {
  adddlert("Welcome guest!")
  }
catch(err)
  {
  txt="¦¹­¶­±¦s¦b¤@­Ó¿ù»~¡C\n\n"
  txt+="¿ù»~´y­z: " + err.description + "\n\n"
  txt+="ÂIÀ»OKÄ~Äò¡C\n\n"
  alert(txt)
  }
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>





<!-- try catch 2 with more handle -------------------------------------->



<html>
<head>
<script type="text/javascript">
var txt=""
function message()
{
try
  {
  adddlert("Welcome guest!")
  }
catch(err)
  {
  txt="There was an error on this page.\n\n"
  txt+="Click OK to continue viewing this page,\n"
  txt+="or Cancel to return to the home page.\n\n"
  if(!confirm(txt))
    {
    document.location.href="http://www.ptpress.com.cn/"
    }
  }
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>



沒有留言:

張貼留言