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

Generator.prototype.throw() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Generator.prototype.throw()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2016 9.

throw() throw .

In this article

js
generatorObject.throw(exception);

exception

. Error (instanceof Error) .

try...catch yield .

done

.

  • true.
  • false.
value

yield .

try...catch throw() exception .

throw() , throw exception; . exception throw() . throw(exception) throw. 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 }

Specification
ECMAScript 2027 LanguageSpecification
# sec-generator.prototype.throw


Web Proxy Viewer  |  New URL  |  Original Page