JS Throw

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>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值