JavaScript 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. The exception can be a string, integer, Boolean or an object.

The example determines the value of a variable called x. A proper error message is displayed if the value of x is
  • lower than or equal to 0,
  • not a number, or
  • not an integer.
If the value is a real number, only the integer part is extracted.



   

Demonstration
The following demonstration shows how the script of HTML and JavaScript is displayed on the Web.