@@ -18,6 +18,7 @@ export = () => {
18
18
return exception ;
19
19
}
20
20
expect ( foo ( ) ) . to . be . a ( "string" ) ;
21
+ expect ( tostring ( foo ( ) ) . find ( "bar" ) [ 0 ] ) . to . be . ok ( ) ;
21
22
} ) ;
22
23
23
24
it ( "should support try/catch with throwing objects" , ( ) => {
@@ -33,13 +34,35 @@ export = () => {
33
34
expect ( foo ( ) ) . to . be . a ( "table" ) ;
34
35
} ) ;
35
36
36
- it ( "should support try/catch with return" , ( ) => {
37
+ it ( "should support try/catch with return in try block " , ( ) => {
37
38
function foo ( ) : unknown {
38
39
try {
39
40
return "foo" ;
40
41
} catch ( e ) { }
41
42
}
42
- expect ( foo ( ) ) . to . be . a ( "string" ) ;
43
+ expect ( foo ( ) ) . to . equal ( "foo" ) ;
44
+ } ) ;
45
+
46
+ it ( "should support try/catch with return in catch block" , ( ) => {
47
+ function foo ( ) : unknown {
48
+ try {
49
+ throw undefined ;
50
+ } catch {
51
+ return "foo" ;
52
+ }
53
+ }
54
+ expect ( foo ( ) ) . to . equal ( "foo" ) ;
55
+ } ) ;
56
+
57
+ it ( "should support try/catch with return in finally block" , ( ) => {
58
+ function foo ( ) : unknown {
59
+ try {
60
+ return 1 ;
61
+ } finally {
62
+ return 2 ;
63
+ }
64
+ }
65
+ expect ( foo ( ) ) . to . equal ( 2 ) ;
43
66
} ) ;
44
67
45
68
it ( "should support try/catch with break" , ( ) => {
@@ -181,4 +204,164 @@ export = () => {
181
204
182
205
expect ( foo ( ) ) . to . equal ( 2 ) ;
183
206
} ) ;
207
+
208
+ it ( "should rethrow the error if there's no catch block" , ( ) => {
209
+ function foo ( ) {
210
+ try {
211
+ throw "bar" ;
212
+ } finally {
213
+ }
214
+ }
215
+
216
+ expect ( ( ) => foo ( ) ) . to . throw ( "bar" ) ;
217
+ } ) ;
218
+
219
+ it ( "should run try -> catch -> finally in order" , ( ) => {
220
+ const array = new Array < number > ( ) ;
221
+ try {
222
+ let condition = true ;
223
+ try {
224
+ array . push ( 1 ) ;
225
+ if ( condition ) throw "error" ;
226
+ array . push ( 999 ) ;
227
+ } catch {
228
+ array . push ( 2 ) ;
229
+ } finally {
230
+ array . push ( 3 ) ;
231
+ }
232
+ } catch { }
233
+
234
+ expect ( array [ 0 ] ) . to . equal ( 1 ) ;
235
+ expect ( array [ 1 ] ) . to . equal ( 2 ) ;
236
+ expect ( array [ 2 ] ) . to . equal ( 3 ) ;
237
+ } ) ;
238
+
239
+ it ( "should run try -> finally in order" , ( ) => {
240
+ const array = new Array < number > ( ) ;
241
+ try {
242
+ let condition = true ;
243
+ try {
244
+ array . push ( 1 ) ;
245
+ if ( condition ) throw "error" ;
246
+ array . push ( 999 ) ;
247
+ } finally {
248
+ array . push ( 2 ) ;
249
+ }
250
+ } catch { }
251
+
252
+ expect ( array [ 0 ] ) . to . equal ( 1 ) ;
253
+ expect ( array [ 1 ] ) . to . equal ( 2 ) ;
254
+ } ) ;
255
+
256
+ it ( "should run finally even if catch throws" , ( ) => {
257
+ let ranFinally = false ;
258
+ try {
259
+ try {
260
+ throw "try error" ;
261
+ } catch {
262
+ throw "catch error" ;
263
+ } finally {
264
+ ranFinally = true ;
265
+ }
266
+ } catch { }
267
+
268
+ expect ( ranFinally ) . to . equal ( true ) ;
269
+ } ) ;
270
+
271
+ it ( "should throw if finally throws" , ( ) => {
272
+ function foo ( ) {
273
+ try {
274
+ } finally {
275
+ throw "bar" ;
276
+ }
277
+ }
278
+
279
+ expect ( ( ) => foo ( ) ) . to . throw ( "bar" ) ;
280
+ } ) ;
281
+
282
+ it ( "should discard errors if finally has a control flow statement" , ( ) => {
283
+ function tryErrorReturn ( ) {
284
+ try {
285
+ throw "try error" ;
286
+ } finally {
287
+ return true ;
288
+ }
289
+ return false ;
290
+ }
291
+
292
+ expect ( tryErrorReturn ( ) ) . to . equal ( true ) ;
293
+
294
+ function catchErrorReturn ( ) {
295
+ try {
296
+ throw "try error" ;
297
+ } catch {
298
+ throw "catch error" ;
299
+ } finally {
300
+ return true ;
301
+ }
302
+ return false ;
303
+ }
304
+
305
+ expect ( catchErrorReturn ( ) ) . to . equal ( true ) ;
306
+
307
+ function tryErrorBreak ( ) {
308
+ for ( let i = 0 ; i < 10 ; i ++ ) {
309
+ try {
310
+ throw "try error" ;
311
+ } finally {
312
+ break ;
313
+ }
314
+ return false ;
315
+ }
316
+ return true ;
317
+ }
318
+
319
+ expect ( tryErrorBreak ( ) ) . to . equal ( true ) ;
320
+
321
+ function catchErrorBreak ( ) {
322
+ for ( let i = 0 ; i < 1 ; i ++ ) {
323
+ try {
324
+ throw "try error" ;
325
+ } catch {
326
+ throw "catch error" ;
327
+ } finally {
328
+ break ;
329
+ }
330
+ return false ;
331
+ }
332
+ return true ;
333
+ }
334
+
335
+ expect ( catchErrorBreak ( ) ) . to . equal ( true ) ;
336
+
337
+ function tryErrorContinue ( ) {
338
+ for ( let i = 0 ; i < 1 ; i ++ ) {
339
+ try {
340
+ throw "try error" ;
341
+ } finally {
342
+ continue ;
343
+ }
344
+ return false ;
345
+ }
346
+ return true ;
347
+ }
348
+
349
+ expect ( tryErrorContinue ( ) ) . to . equal ( true ) ;
350
+
351
+ function catchErrorContinue ( ) {
352
+ for ( let i = 0 ; i < 1 ; i ++ ) {
353
+ try {
354
+ throw "try error" ;
355
+ } catch {
356
+ throw "catch error" ;
357
+ } finally {
358
+ continue ;
359
+ }
360
+ return false ;
361
+ }
362
+ return true ;
363
+ }
364
+
365
+ expect ( catchErrorContinue ( ) ) . to . equal ( true ) ;
366
+ } ) ;
184
367
} ;
0 commit comments