Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.
The throw() method of Generator instances acts as if a throw statement is inserted in the generator's body at the current suspended position, which informs the generator of an error condition and allows it to handle the error, or perform cleanup and close itself.
The exception to throw. For debugging purposes, it is useful to make it an instanceof Error.
If the thrown exception is caught by a try...catch and the generator resumes to yield more values, it will return an Object with two properties:
doneA boolean value:
The value yielded from the next yield expression.
Thrown if the generator is already running.
If the exception is not caught by a try...catch within the generator function, it is also thrown to the caller of throw().
The throw() method, when called, can be seen as if a throw exception; statement is inserted in the generator's body at the current suspended position, where exception is the exception passed to the throw() method. Therefore, in a typical flow, calling throw(exception) will cause the generator to throw. However, if the yield expression is wrapped in a try...catch block, the error may be caught and control flow can either resume after error handling, or exit gracefully.
The following example shows a generator and an error that is thrown using the throw method. An error can be caught by a try...catch block as usual.
| ECMAScript® 2027 Language Specification # sec-generator.prototype.throw |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 10, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.