1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
---
title: throw
slug: Web/JavaScript/Reference/Statements/throw
translation_of: Web/JavaScript/Reference/Statements/throw
tags:
- Exception
- JavaScript
- Language feature
- Statement
browser-compat: javascript.statements.throw
---
<div>{{jsSidebar("Statements")}}</div>
<p><strong><code>throw</code></strong>문은 사용자 정의 예외를 발생(throw)할 수 있습니다. 예외가 발생하면 현재 함수의 실행이 중지되고 (<code>throw</code> 이후의
명령문은 실행되지 않습니다.), 제어 흐름은 콜스택의 첫 번째 <a href="/ko/docs/Web/JavaScript/Reference/Statements/try...catch"><code>catch</code></a>
블록으로 전달됩니다. 호출자 함수 사이에 <code>catch</code> 블록이 없으면 프로그램이 종료됩니다. </p>
<div>{{EmbedInteractiveExample("pages/js/statement-throw.html")}}</div>
<h2 id="Syntax">문법</h2>
<pre class="brush: js">throw <var>expression</var>; </pre>
<dl>
<dt><code>expression</code></dt>
<dd>예외를 발생시킬 표현식</dd>
</dl>
<h2 id="Description">설명</h2>
<p>예외를 발생하기 위해 <code>throw</code> 문을 사용하세요. 예외를 발생시키면 <code><var>expression</var></code>은 예외 값을 지정합니다.
다음 각각은 예외를 발생시킵니다:</p>
<pre class="brush: js">throw 'Error2'; // 문자열 값을 가지는 예외가 발생합니다.
throw 42; // 42 값을 가진 예외가 발생합니다.
throw true; // true 값을 가지는 예외가 발생합니다.</pre>
<p>또한 <code>throw</code> 문은 <a href="/ko/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion">
자동 세미콜론 삽입</a> (ASI)에 의해 영향을 받으며 <code>throw</code> 키워드와 표현식 사이에 줄 종결자는 허용되지 않으므로 주의해야합니다.</p>
<h2 id="Examples">예제</h2>
<h3 id="Throw_an_object">예외 값으로 객체 사용하기</h3>
<p>예외를 발생시킬 때 객체를 명시할 수 있습니다. 그러면 <code>catch</code> 블록에서 객체의 속성을 참조 할 수 있습니다.
다음 예제에서는 <code>UserException</code> 타입의 객체를 만들고 <code>throw</code> 구문에서 이 객체를 사용합니다.</p>
<pre class="brush: js">function UserException(message) {
this.message = message;
this.name = 'UserException';
}
function getMonthName(mo) {
mo = mo - 1; // 월 숫자를 배열의 인덱스 값과 맞추기 위해서 입니다.(1 = 1월, 12 = 12월)
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
if (months[mo] !== undefined) {
return months[mo];
} else {
throw new UserException('InvalidMonthNo');
}
}
try {
// try 문
var myMonth = 15; // 15 는 범위를 벗어났기 때문에 예외를 발생시킵니다
var monthName = getMonthName(myMonth);
} catch (e) {
monthName = 'unknown';
console.error(e.message, e.name); // 오류 처리기에 예외 객체를 전달합니다
}
</pre>
<h3 id="Another_example_of_throwing_an_object">예외 값으로 객체 사용하는 다른 예제</h3>
<p>다음 예제는 입력 문자열에서 미국 우편 번호를 테스트합니다.<br>
우편 번호가 잘못된 형식을 사용하는 경우 throw 문은 <code>ZipCodeFormatException</code> 타입의 객체를 만들어 예외를 발생시킵니다.</p>
<pre class="brush: js">/*
* ZipCode 객체를 만듭니다.
*
* 입력받을 수 있는 우편번호의 형태는 아래와 같습니다:
* 12345
* 12345-6789
* 123456789
* 12345 6789
*
* 만약 ZipCode 생성자로 전달된 매개변수가 이 패턴 중 하나도 맞지 않으면,
* 예외를 발생시킵니다.
*/
function ZipCode(zip) {
zip = new String(zip);
pattern = /[0-9]{5}([- ]?[0-9]{4})?/;
if (pattern.test(zip)) {
// 우편번호 값은 문자열의 첫 번째 매칭일 것입니다.
this.value = zip.match(pattern)[0];
this.valueOf = function() {
return this.value
};
this.toString = function() {
return String(this.value)
};
} else {
throw new ZipCodeFormatException(zip);
}
}
function ZipCodeFormatException(value) {
this.value = value;
this.message = 'does not conform to the expected format for a zip code';
this.toString = function() {
return this.value + this.message;
};
}
/*
* 이것은 미국 주소에 대한 주소 데이터를 검증하는 스크립트에서
* 발생할 수 있습니다.
*/
const ZIPCODE_INVALID = -1;
const ZIPCODE_UNKNOWN_ERROR = -2;
function verifyZipCode(z) {
try {
z = new ZipCode(z);
} catch (e) {
if (e instanceof ZipCodeFormatException) {
return ZIPCODE_INVALID;
} else {
return ZIPCODE_UNKNOWN_ERROR;
}
}
return z;
}
a = verifyZipCode(95060); // 95060 반환
b = verifyZipCode(9560); // -1 반환
c = verifyZipCode('a'); // -1 반환
d = verifyZipCode('95060'); // 95060 반환
e = verifyZipCode('95060 1234'); // 95060 1234 반환
</pre>
<h3 id="Rethrow_an_exception">예외 다시 발생시키기</h3>
<p><code>throw</code>를 사용하여 예외를 잡은(catch) 후에 예외를 다시 발생시킬 수 있습니다.
다음 예제에서는 숫자 값으로 예외를 잡지만 값이 50 이상이면 예외를 다시 발생시킵니다.
반환된 예외는 둘러싸는 함수 또는 최상위 수준으로 전파되어 사용자가 볼 수 있도록합니다</p>
<pre class="brush: js">try {
throw n; // 숫자 값으로 예외를 발생시킵니다.
} catch (e) {
if (e <= 50) {
// 1-50 사이의 예외를 처리하는 구문
} else {
// 이 예외를 처리할 수 없어서, 다시 예외를 발생시킵니다
throw e;
}
}
</pre>
<h2 id="Specifications">명세</h2>
{{Specifications}}
<h2 id="Browser_compatibility">브라우저 호환성</h2>
<p>{{Compat}}</p>
<h2 id="See_also">같이 보기</h2>
<ul>
<li>{{jsxref("Statements/try...catch", "try...catch")}}</li>
<li>{{jsxref("Global_Objects/Error", "Error")}}</li>
</ul>
|