Generator.prototype.throw()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2016ë 9ì.
ì ëë ì´í°ì throw() ë©ìëë íì¬ ì¤ë¨ ë ìì¹ìì ì ëë ì´í°ì throw ë¬¸ì´ ì½ì
ëë ê²ì²ë¼ ìëíì¬ ì ëë ì´í°ì ì¤ë¥ ì¡°ê±´ì ìë ¤ì£¼ê³ ì¤ë¥ë¥¼ ì²ë¦¬íê±°ë ì 리 ìì
ì ìííë©° ì ëë ì´í°ë¥¼ ì¢
ë£í ì ìëë¡ í©ëë¤.
구문
generatorObject.throw(exception);
매ê°ë³ì
ë°í ê°
ìì¸ê° try...catchìì ë°ìíê³ ì ëë ì´í°ê° ì¬ê°íì¬ ë ë§ì ê°ì yieldíë©´ ë¤ì ë ê°ì§ ìì±ì ê°ì§ ê°ì²´ë¥¼ ë°íí©ëë¤.
ìì¸
ìì¸ê° try...catchìì ë°ìíì§ ìì¼ë©´ throw()ë¡ ì ë¬ ë exceptionì ì ëë ì´í°ìì ì·¨ê¸íì§ ììµëë¤.
ì¤ëª
throw() ë©ìëë í¸ì¶ ë ë, ì´ë íì¬ ì¤ë¨ ë ìì¹ì ì ëë ì´í°ì ì½ì
ë throw exception; 문 ì²ë¼ ë³´ì¼ ì ììµëë¤. exceptionì throw() ë©ìëì ì ë¬ ë ìì¸ì
ëë¤. ë°ë¼ì ì¼ë°ì ì¸ íë¦ìì throw(exception)ì í¸ì¶íë©´ ì ëë ì´í°ê° throwë©ëë¤. ê·¸ë¬ë yield ìì´ try...catch ë¸ë¡ì¼ë¡ ê°ì¸ì¡ ë¤ë©´, ì¤ë¥ë¥¼ í¬ì°©í ì ìì¼ë©° ì ì´ íë¦ì ì¤ë¥ ì²ë¦¬ í ì¬ê°íê±°ë ì ìì ì¼ë¡ ì¢
ë£ íëë¡ ì§íë©ëë¤.
ìì
>throw() ì¬ì©í기
ë¤ì ìì ììë ê°ë¨í ì ëë ì´í°ì throw ë©ìë를 ì¬ì©íì¬ ì¤ë¥ë¥¼ ë°ììí¤ë ê²ì ë³´ì¬ ì¤ëë¤. ì¤ë¥ë ë³´íµ try...catch ë¸ë¡ì¼ë¡ ì²ë¦¬í©ëë¤.
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 Language Specification> # sec-generator.prototype.throw> |