ÍøÂçѧԺ

w3pop.com :: ÍøÂçѧԺ :: JavaScript :: JS Throw

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

JavaScript
JSÊýѧ¶ÔÏó²Î¿¼
JS×Ö·û´®¶ÔÏó²Î¿¼
JSº¯Êý²Î¿¼
JSʼþ²Î¿¼
Javascript ³£ÓÃÕ..
FFºÍIEϵÄjs¼æÈÝ..
jQuery ¼òµ¥½éÉÜ
jQuery / ºËÐÄ / ..
jQuery / ºËÐÄ / ..
ÈçºÎʹÓÃJSÀ´ÅжÏ..
JavascriptÔÚIEºÍ..
3¸öjs×Ö·û±àÂ뺯Ê..
javascript ÖÐµÄ ..

JS Throw


×÷Õß:w3pop.com ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-04-28 ÐÞ¸Ä:2007-12-03 ä¯ÀÀ:12981 :: ::

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