JavaScript throw Statement
The throw statement raises an exception, stopping the current function and passing control to the nearest catch block.
Description
The throw statement throws a user-defined exception. Execution of the current function stops, and control passes to the first catch block in the call stack; if there is none, the script reports an uncaught error. You can throw any value, but throwing an Error (or a subclass) gives a stack trace and a consistent message.
Inside an async function, throw rejects the returned promise.
Throwing stops execution immediately, so code after the throw in the same block never runs. Throw errors for situations the current function genuinely can't handle, such as invalid arguments, and let calling code decide how to recover. Custom error classes that extend Error make it easy to tell different problems apart with instanceof, and the cause option keeps the original error when you rethrow a more meaningful one.
Syntax
throw expression;Example
Browser Support
The following information will show you the current browser support for the throw statement. Hover over a browser icon to see the version that first introduced support for this statement.
Desktop





Tablets & Mobile





