JavaScript Throw statement

Source: Internet
Author: User

In JavaScript try...catch ... Statement, we learned how to catch a system-thrown error. JavaScript allows us to customize the error and throw the error message.

Throw statement

The throw statement allows us to create custom errors.

The correct technical term is: Create or throw an exception (exception).

If you use throw with try and catch, you can control the flow and generate custom error messages.

Throw statement syntax

Throw exception

An exception can be a JavaScript string, a number, a logical value, or an object.

In the following example, we define a division calculation function that throws an exception if the dividend of the function is 0. At the time of the call with Try...catch ... To handle:

<! DOCTYPE html>//Trigger Operation        functiondivsion (NUM1, num2) {if(num2 = = 0) {                Throw"Dividend cannot be 0"; }            returnNUM1/num2; }        functionBtn_click () {varNUM1 = number (document.getElementById ("TxtNum1"). Value); varnum2 = number (document.getElementById ("txtNum2"). Value); Try            {                varresult =divsion (NUM1, num2); document.getElementById ("Txtresult"). Value =result; }            Catch(ERR) {alert (ERR); }        }    </script>

JavaScript Throw statement

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.