Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016년 9월.
제너레이터의 throw() 메서드는 현재 중단 된 위치에서 제너레이터에 throw 문이 삽입되는 것처럼 작동하여 제너레이터의 오류 조건을 알려주고 오류를 처리하거나 정리 작업을 수행하며 제너레이터를 종료할 수 있도록 합니다.
발생시킬 예외입니다. 디버깅을 위해 Error 인스턴스(instanceof Error)로 만드는 것이 유용합니다.
예외가 try...catch에서 발생하고 제너레이터가 재개하여 더 많은 값을 yield하면 다음 두 가지 속성을 가진 객체를 반환합니다.
done불리언 값입니다.
다음 yield 식에서 산출된 값입니다.
예외가 try...catch에서 발생하지 않으면 throw()로 전달 된 exception은 제너레이터에서 취급하지 않습니다.
throw() 메소드는 호출 될 때, 이는 현재 중단 된 위치의 제너레이터에 삽입된 throw exception; 문 처럼 보일 수 있습니다. exception은 throw() 메서드에 전달 된 예외입니다. 따라서 일반적인 흐름에서 throw(exception)을 호출하면 제너레이터가 throw됩니다. 그러나 yield 식이 try...catch 블록으로 감싸졌 다면, 오류를 포착할 수 있으며 제어 흐름은 오류 처리 후 재개하거나 정상적으로 종료 하도록 진행됩니다.
다음 예제에서는 간단한 제너레이터와 throw 메서드를 사용하여 오류를 발생시키는 것을 보여 줍니다. 오류는 보통 try...catch 블록으로 처리합니다.
| ECMAScript® 2027 Language Specification # sec-generator.prototype.throw |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2023년 8월 8일 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.