[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Generator/throw [Back]  [Original]

Generator.prototype.throw() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Generator.prototype.throw()

Baseline

20169

throw() Generator throw

js
generatorInstance.throw(exception)

exception

instanceof Error

2 Object

done

  • true
  • false
value

yield

TypeError

exception try...catch throw()

throw() throw exception; exception throw() throw(exception) yield try...catch

throw()

throw try...catch

js
function* gen() {
  while (true) {
    try {
      yield 42;
    } catch (e) {
      console.log("Error caught!");
    }
  }
}

const g = gen();
g.next();
// { value: 42, done: false }
g.throw(new Error("Something went wrong"));
// "Error caught!"
// { value: 42, done: false }

ECMAScript 2027 LanguageSpecification
# sec-generator.prototype.throw


Web Proxy Viewer  |  New URL  |  Original Page