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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
---
title: throw
slug: Web/JavaScript/Reference/Statements/throw
tags:
- Ausnahmen
- JavaScript
- exceptions
- throw
translation_of: Web/JavaScript/Reference/Statements/throw
---
<div>
<div>{{jsSidebar("Statements")}}</div>
</div>
<h2 id="Zusammenfassung">Zusammenfassung</h2>
<p>Die <strong><code>throw</code> Anweisung</strong> "wirft" eine benutzerdefinierte Exception (Ausnahme). Die Programmausführung der aktuellen Funktion bzw. des globalen Codes wird angehalten (Anweisungen nach <code>throw</code> werden dann nicht mehr ausgeführt), und die Programmausführung wird an den nächsthöheren <a href="/en-US/docs/Web/JavaScript/Reference/Statements/try...catch"><code>catch</code></a> Block in der Ausführungshierarchie übergeben. Falls in der gesamten Ausführungschierarchie kein <code>catch</code> Block definiert wurde, wird das Programm komplett angehalten.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox">throw <em>ausdruck</em>; </pre>
<dl>
<dt><code>ausdruck</code></dt>
<dd>Die zu werfende Ausnahme.</dd>
</dl>
<h2 id="Beschreibung">Beschreibung</h2>
<p>Verwenden Sie die <code>throw</code> Anweisung, um eine Ausnahme zu werfen. Wenn Sie eine Ausnahme werfen, bestimmt <code>ausdruck</code> den Typ der Ausnahme. Als Typ können Objekte (siehe Beispiel unten), als auch skalare Werte, wie Zeichenketten, Zahlen und boolesche Werte verwendet werden. Folgende Beispiele werfen jeweils eine Ausnahme:</p>
<pre class="brush: js">throw "Fehler"; // erzeugt Ausnahme mit dem Wert "Fehler" (als String)
throw 42; // erzeugt Ausnahme mit dem Wert 42 (als Integer)
throw true; // erzeugt Ausnahme mit dem Wert TRUE (als boolescher Wert)</pre>
<p>Beachten Sie auch, dass die <code>throw</code>-Anweisung von der <a href="/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Automatic_semicolon_insertion">automatic semicolon insertion (ASI)</a> betroffen ist und daher kein Zeilenumbruch zwischen dem <code>throw</code> Schlüsselwort und dem Ausdruck zulässig ist.</p>
<h2 id="Beispiele">Beispiele</h2>
<h3 id="Objekt_als_Ausnahme_werfen">Objekt als Ausnahme werfen</h3>
<p>Als Ausnahme kann auch ein Objekt geworfen werden. Auf Eigenschaften des geworfenen Objektes kann dann im <code>catch</code> Block zugegriffen werden. Im folgenden Beispiel wird in der Funktion <code>divide</code> versucht Zahl <code>n</code> durch <code>m</code> zu dividieren. Falls <code>m</code> die Zahl 0 ist, wird ein Objekt (als Funktion) vom Typ <code>DivisionException</code> erzeugt und als Ausnahme geworfen, da Teilen durch 0 nicht möglich ist.</p>
<pre class="brush: js">function DivisionException(message) {
this.message = message;
this.name = "DivisionException";
}
function divide(n, m) {
if (m == 0) {
throw new DivisionException("Teilen durch Null nicht möglich");
}
return n / m;
}
try {
// versuche 5 / 2 zu teilen
alert(divide(5, 2)); // 2.5
// versuche 5 / 0 zu teilen
var ausgabe = divide(5, 0);
// diese Zeile wird nie erreicht
alert(ausgabe);
}
catch (e) {
// "DivisionException: Teilen durch Null nicht möglich"
alert(e.name + ": " + e.message);
}
</pre>
<p>Ausnahmen werden in der Regel als Funktionen definiert und mit dem <code>new</code> Schlüsselwort als Objekte instantiiert, da es in JavaScript kein direktes Konzept von Konstruktoren gibt, wie in anderen Programmiersprachen. Die Funktion dient dann selbst als Konstruktor. Falls in der <code>catch</code> Anweisung keine Unterscheidung der Ausnahme benötigt wird (z. B. mit Hilfe des <code>instanceof</code> Operators - siehe Beispiel unten), kann das Objekt direkt übergeben werden. Aus praktischen Gründen kann im geworfenen Objekt die Methode <a href="/de/docs/Web/JavaScript/Reference/Global_Objects/Object/toString">toString</a> definiert werden, um es später einfacher ausgeben zu können:</p>
<pre class="brush: js">try {
throw {
name: "MyException",
message: "Something is wrong",
toString: function() {
return this.name + ": " + this.message;
}
};
}
catch (e) {
alert(e); // "MyException: Something is wrong"
}</pre>
<h3 id="Unterschiedliche_Ausnahmen_werfen_und_unterscheiden">Unterschiedliche Ausnahmen werfen und unterscheiden</h3>
<p>Es ist möglich unterschiedliche Ausnahmen in einem try Block zu werfen und im dazugehörigen catch Block abzufangen. Dies erlaubt ein gezielteres Abfangen von Ausnahmen.</p>
<pre class="brush: js">function myException(message) {
this.message = message;
};
function myOtherException(text) {
this.text = text;
};
var exceptions = [
"Don't panic! Where is my towel?",
42,
new myException("So long, and thanks for all the fish!"),
new myOtherException("Deep Thought is still watching..."),
];
for (var i=0; i < exceptions.length; i++) {
try {
throw exceptions[i];
}
catch (e) {
if (typeof e === "string") {
alert('String exception: ' + e);
}
else if (typeof e === "number") {
alert('Number exception: ' + e);
}
else if (typeof e === "object")
{
if (e instanceof myException) {
alert("myException: " + e.message);
}
else if (e instanceof myOtherException) {
alert("myOtherException: " + e.text);
}
}
}
}
</pre>
<h3 id="Abgefangene_Ausnahmen_neu_werfen">Abgefangene Ausnahmen neu werfen</h3>
<p>Die <code>throw</code> Anweisung kann auch innerhalb eines <code>catch</code> Blocks verwendet werden, um eine neue oder die bereits abgefangene Ausnahme erneut zu werfen. Alle Anweisungen folgenden Anweisungen im <code>catch</code> Block werden wieder nicht ausgeführt und die neu geworfene Ausnahme wird an die nächsthöheren <code>catch</code> Block übergeben. Somit können "Ebenen" entworfen werden, die jeweils nur bestimmte Ausnahmen behandeln.</p>
<p>Folgender Quelltext zeigt <code>"myException: This is an exception!"</code> an, da der innere <code>catch</code> Block nicht für Ausnahmen vom Typ <code>myOtherException</code> zuständig ist und daher diese eine Ebene nach oben weiterleitet.</p>
<pre class="brush: js">function myException(message) {
this.message = message;
};
function myOtherException(text) {
this.text = text;
};
try {
try {
throw new myOtherException("This is an exception!");
}
catch (e) {
if (e instanceof myException) {
alert("myException: " + e.message); // wird nicht ausgeführt
} else {
throw e; // eine Ebene nach oben weiterleiten
}
}
}
catch (e) {
alert("myException: " + e.text);
}
</pre>
<div class="note">
<p>Falls eine Exception durch keinen catch Block abgefangen wird, wird die Ausnahme in der JavaScript Konsole ausgegeben und die Programmausführung komplett unterbrochen.</p>
</div>
<p> </p>
<h2 id="Spezifikationen">Spezifikationen</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Kommentar</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Initiale Definition. Implementiert in JavaScript 1.4</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-12.13', 'throw statement')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-throw-statement', 'throw statement')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-throw-statement', 'throw statement')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browserkompatibilität">Browserkompatibilität</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Statements/try...catch"><code>try...catch</code></a></li>
</ul>
|