blob: a7cb5223d64e5aa3aed66dfc5e187993e28bc962 (
plain) (
blame)
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
|
discard """
output: '''
Generic except: Test
Specific except
Multiple idents in except
Multiple except branches
Multiple except branches 2
'''
targets: "c"
"""
import asyncdispatch, strutils
# Here we are testing the ability to catch exceptions.
proc foobar() {.async.} =
if 5 == 5:
raise newException(IndexError, "Test")
proc catch() {.async.} =
# TODO: Create a test for when exceptions are not caught.
try:
await foobar()
except:
echo("Generic except: ", getCurrentExceptionMsg().splitLines[0])
try:
await foobar()
except IndexError:
echo("Specific except")
try:
await foobar()
except OSError, FieldError, IndexError:
echo("Multiple idents in except")
try:
await foobar()
except OSError, FieldError:
assert false
except IndexError:
echo("Multiple except branches")
try:
await foobar()
except IndexError:
echo("Multiple except branches 2")
except OSError, FieldError:
assert false
waitFor catch()
proc test(): Future[bool] {.async.} =
result = false
try:
raise newException(OSError, "Foobar")
except:
result = true
return
proc foo(): Future[bool] {.async.} = discard
proc test2(): Future[bool] {.async.} =
result = false
try:
discard await foo()
raise newException(OSError, "Foobar")
except:
result = true
return
proc test3(): Future[int] {.async.} =
result = 0
try:
try:
discard await foo()
raise newException(OSError, "Hello")
except:
result = 1
raise
except:
result = 2
return
proc test4(): Future[int] {.async.} =
try:
discard await foo()
raise newException(ValueError, "Test4")
except OSError:
result = 1
except:
result = 2
var x = test()
assert x.waitFor()
x = test2()
assert x.waitFor()
var y = test3()
assert y.waitFor() == 2
y = test4()
assert y.waitFor() == 2
|