网络学院 w3pop社区 网络资源 IT新闻

w3pop.com :: 网络学院 :: JavaScript :: JS Throw

会员登陆

帐号

密码

回答

记住密码

忘记密码? 注册

JavaScript
JS 介绍
JS 怎样使用
JS 在哪使用
JS 变量
JS If...Else
JS Switch
JS 操作符
JS Popup Boxes
JS 函数
JS For 循环
JS While 循环
JS Break 循环
JS For...In
JS 事件
JS Try...Catch
JS Throw
JS onerror
JS 特殊字符
JS Guidelines
JS 对象介绍

JS Throw


作者:w3pop.com 翻译/整理:w3pop.com 发布:2007-04-28 修改:2007-12-03 浏览:7215 :: ::

The throw statement allows you to create an exception.
throw声明可以让你创建一个例外情况。


Examples
实例

The throw statement [throw语句]]
How to use the throw statement.
怎样使用throw声明


The Throw Statement

The throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages.
如果你把throw声明和try...catch声明组合起来使用,你可以控制程序流程并引出精确的错误信息

Syntax语法

throw(exception)

The exception can be a string, integer, Boolean or an object.
例外可以是串,整数,布尔或是对象。

Note that throw is written in lowercase letters. Using uppercase letters will generate a JavaScript error!
注意throw是要小写的,不然会出错

Example 1
例子1

The example below determines the value of a variable called x. If the value of x is higher than 10 or lower than 0 we are going to throw an error. The error is then caught by the catch argument and the proper error message is displayed:
下面的例子(结果)决定于变量x的值。如果x的值大于10或是小于0就出现不同的错误提示

<html>
<body>
<script type="text/javascript">
var x=prompt("Enter a number between 0 and 10:","")
try
{
if(x>10)
throw "Err1"

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>
</body>

</html>

评论 (5) 1 All