summary refs log tree commit diff stats
path: root/tests/exception/texceptions.nim
blob: 62d24c9341cc27a49236c9fbad7d3f6841c37ebc (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
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
discard """
  disabled: "windows" # no sigsetjmp() there
  matrix: "-d:nimStdSetjmp; -d:nimSigSetjmp; -d:nimRawSetjmp; -d:nimBuiltinSetjmp"
  output: '''

BEFORE
FINALLY

BEFORE
EXCEPT
FINALLY
RECOVER

BEFORE
EXCEPT: IOError: hi
FINALLY
'''
"""

echo ""

proc no_exception =
  try:
    echo "BEFORE"

  except:
    echo "EXCEPT"
    raise

  finally:
    echo "FINALLY"

try: no_exception()
except: echo "RECOVER"

echo ""

proc reraise_in_except =
  try:
    echo "BEFORE"
    raise newException(IOError, "")

  except IOError:
    echo "EXCEPT"
    raise

  finally:
    echo "FINALLY"

try: reraise_in_except()
except: echo "RECOVER"

echo ""

proc return_in_except =
  try:
    echo "BEFORE"
    raise newException(IOError, "hi")

  except:
    echo "EXCEPT: ", getCurrentException().name, ": ", getCurrentExceptionMsg()
    return

  finally:
    echo "FINALLY"

try: return_in_except()
except: echo "RECOVER"

block: #10417
  proc moo() {.noreturn.} = discard

  let bar =
    try:
      1
    except:
      moo()

  doAssert(bar == 1)

# Make sure the VM handles the exceptions correctly
block:
  proc fun1(): seq[int] =
    try:
      try:
        raise newException(ValueError, "xx")
      except:
        doAssert("xx" == getCurrentExceptionMsg())
        raise newException(KeyError, "yy")
    except:
      doAssert("yy" == getCurrentExceptionMsg())
      result.add(1212)
    try:
      try:
        raise newException(AssertionDefect, "a")
      finally:
        result.add(42)
    except AssertionDefect:
      result.add(99)
    finally:
      result.add(10)
    result.add(4)
    result.add(0)
    try:
      result.add(1)
    except KeyError:
      result.add(-1)
    except ValueError:
      result.add(-1)
    except IndexDefect:
      result.add(2)
    except:
      result.add(3)

    try:
      try:
        result.add(1)
        return
      except:
        result.add(-1)
      finally:
        result.add(2)
    except KeyError:
      doAssert(false)
    finally:
      result.add(3)

  let x1 = fun1()
  const x2 = fun1()
  doAssert(x1 == x2)