Standard built-ins

Generator.prototype.throw()

Try the free JavaScript compiler

Injects an exception into the generator at its suspended position. A catch may handle it; otherwise it propagates.

Formal Syntax & Structure

javascript syntaxjavascript
generator.throw([value])

Parameters & Attributes

NameTypeDescription
valueanyInput or completion/exception value; optional.

Return Value

object

Iterator result object.

Working Example

Example Snippetjavascript
function* g(){try{yield 1;}catch(e){yield e.message;}}const i=g();i.next();console.log(i.throw(new Error("x")).value);
JavaScript Reference