Files
Nim/tests/destructor/tgotoexceptions.nim
Andreas Rumpf c3344862b0 --exception:goto switch for deterministic exception handling (#12977)
This implements "deterministic" exception handling for Nim based on goto instead of setjmp. This means raising an exception is much cheaper than in C++'s table based implementations. Supports hard realtime systems. Default for --gc:arc and the C target because it's generally a good idea and arc is all about deterministic behavior.

Note: This implies that fatal runtime traps are not catchable anymore! This needs to be documented.
2020-01-01 10:01:49 +01:00

118 lines
1.6 KiB
Nim
Executable File

discard """
output: '''
msg1
msg2
finally2
finally1
begin
one iteration!
caught!
except1
finally1
caught! 2
BEFORE
FINALLY
BEFORE
EXCEPT
FINALLY
RECOVER
BEFORE
EXCEPT: IOError: hi
FINALLY
'''
cmd: "nim c --gc:arc --exceptions:goto $file"
"""
#bug 7204
proc nested_finally =
try:
raise newException(KeyError, "msg1")
except KeyError as ex:
echo ex.msg
try:
raise newException(ValueError, "msg2")
except:
echo getCurrentExceptionMsg()
finally:
echo "finally2"
finally:
echo "finally1"
nested_finally()
proc doraise =
raise newException(ValueError, "gah")
proc main =
while true:
try:
echo "begin"
doraise()
finally:
echo "one ", "iteration!"
try:
main()
except:
echo "caught!"
when true:
proc p =
try:
raise newException(Exception, "Hello")
except:
echo "except1"
raise
finally:
echo "finally1"
try:
p()
except:
echo "caught! 2"
proc noException =
try:
echo "BEFORE"
except:
echo "EXCEPT"
raise
finally:
echo "FINALLY"
try: noException()
except: echo "RECOVER"
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"
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"