您的位置:首页 > Web前端 > JavaScript

javascript 中的异常处理

2006-04-12 16:46 387 查看
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>测试javascript中的trycatchfinally</title>

<script language="javascript">
<!--
function initException(Num,Msg)//define an Exception(define an new object)
{
this.ErrorNumber=Num;//error's number
this.ErrorMessage=Msg;//error's message
}
function CreateException()
{
ex=new initException(1,"Created!");//create the excepion (create an new instance of initException)
throw ex;//throw ex
}
function test()
{
try
{
CreateException();
}
catch(ex)//catch the ex
{
if(ex instanceof initException)//if the exception is our target,do something
{
alert(ex.ErrorNumber+ex.ErrorMessage);
}
else//else throw again
{
throw ex;
}
}
finally
{
alert("end");//do something finally
}
}
-->
</script>

</head>

<body>
<form>
<input type="button" name="testbutton" value="testbutton" onClick="test()">
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: