JavaScript Throw Tutorial

Source: Internet
Author: User

The throw declaration allows you to create an exception.

Example
The Throw statement
How to use throw.

<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";
}
else if (isNaN (x))
{
Throw "ERR3";
}
}
catch (er)
{
if (er== "ERR1")
{
Alert ("error! The value is too high ");
}
if (er== "ERR2")
{
Alert ("error! The value is too low ");
}
if (er== "ERR3")
{
Alert ("error! The value is not a number ");
}
}
</script>



Throw statement
The throw declaration allows you to create an exception. If you use this statement with the attempt ... catch statement, you can control the process and produce an accurate error message.

Grammar
Throw (Exception)

The only exception may be a string, an integer, a Boolean, or an object.

Please note that Throw is writing letters. A JavaScript error is generated in uppercase letters!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.