--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.
This commit is contained in:
parent
8a63caca07
commit
c3344862b0
23 changed files with 640 additions and 183 deletions
117
tests/destructor/tgotoexceptions.nim
Executable file
117
tests/destructor/tgotoexceptions.nim
Executable file
|
|
@ -0,0 +1,117 @@
|
|||
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"
|
||||
Loading…
Add table
Add a link
Reference in a new issue