-
Notifications
You must be signed in to change notification settings - Fork 17
/
trycatch.nim
59 lines (39 loc) 路 778 Bytes
/
trycatch.nim
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
#
# Proof of concept try/catch/throw
#
import cps
type
Tc = ref object of Continuation
var
err: bool
msg: string
where: ContinuationFn
# CPS magic functions
proc start(c: Tc): Tc {.cpsMagic.} =
where = c.fn
return c
proc throw(c: Tc, m: string): Tc {.cpsMagic.} =
err = true
msg = m
c.fn = where
reset c.mom
return c
# This is a cps func that will throw an error
proc deeper() {.cps:Tc.} =
echo "throwing"
throw("Boom")
echo "you will never see this"
# 'main' cps function, sets up try/cach and calls another
# function that will throw
proc foo() {.cps:Tc.} =
start()
if not err:
echo "one"
echo "two"
deeper()
echo "three"
echo "four"
if err:
echo "** caught error: ", msg, " **"
echo "bye"
foo()