Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript throw Statement

JavaScript throw Statement

JavaScript Statement Reference JavaScript statement reference manual

Example

This example checks the value of an input variable. If the value is wrong, an exception (err) is thrown.

The exception (err) is caught by the catch statement, which outputs a custom error message:

Please input a number between 5 and 10:

Try it »


Definition and Usage

The throw statement throws an error.

When an error occurs, JavaScript stops executing and throws an error message.

The technical term for this situation is: JavaScript will throw an error.

throw The statement creates a custom error.

The technical term is: Throwing an Exception.

An exception can be a JavaScript string, number, boolean, or object:

throw "Too big"; // Throw text

throw 500; // Throw a number

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

For more JavaScript error information, please read our JavaScript Errors the tutorial.


Browser support

Statement
throw Yes Yes Yes Yes Yes

Syntax

throw

expression

;

Parameter value

Parameters Description
expression Required. The exception to throw. Can be a string, number, boolean, or object.

Technical Details

JavaScript version: 1.4

Related pages

JavaScript tutorial: JavaScript Errors

JavaScript Reference Manual: JavaScript try/catch/finally Statement


JavaScript Statement Reference JavaScript statement reference manual