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 2015년 7월.
throw 문은 사용자 정의 예외를 발생(throw)할 수 있습니다. 예외가 발생하면 현재 함수의 실행이 중지되고 (throw 이후의 명령문은 실행되지 않습니다.), 제어 흐름은 콜스택의 첫 번째 catch 블록으로 전달됩니다. 호출자 함수 사이에 catch 블록이 없으면 프로그램이 종료됩니다.
예외를 발생시킬 표현식
예외를 발생하기 위해 throw 문을 사용하세요. 예외를 발생시키면 expression은 예외 값을 지정합니다. 다음 각각은 예외를 발생시킵니다:
또한 throw 문은 자동 세미콜론 삽입 (ASI)에 의해 영향을 받으며 throw 키워드와 표현식 사이에 줄 종결자는 허용되지 않으므로 주의해야합니다.
예외를 발생시킬 때 객체를 명시할 수 있습니다. 그러면 catch 블록에서 객체의 속성을 참조 할 수 있습니다. 다음 예제에서는 UserException 타입의 객체를 만들고 throw 구문에서 이 객체를 사용합니다.
다음 예제는 입력 문자열에서 미국 우편 번호를 테스트합니다. 우편 번호가 잘못된 형식을 사용하는 경우 throw 문은 ZipCodeFormatException 타입의 객체를 만들어 예외를 발생시킵니다.
throw를 사용하여 예외를 잡은(catch) 후에 예외를 다시 발생시킬 수 있습니다. 다음 예제에서는 숫자 값으로 예외를 잡지만 값이 50 이상이면 예외를 다시 발생시킵니다. 반환된 예외는 둘러싸는 함수 또는 최상위 수준으로 전파되어 사용자가 볼 수 있도록합니다
| ECMAScript® 2027 Language Specification # sec-throw-statement |
Enable JavaScript to view this browser compatibility table.
This page was last modified on 2025년 2월 11일 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.