tgotoexceptions.nim 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. discard """
  2. output: '''
  3. msg1
  4. msg2
  5. finally2
  6. finally1
  7. begin
  8. one iteration!
  9. caught!
  10. except1
  11. finally1
  12. caught! 2
  13. BEFORE
  14. FINALLY
  15. BEFORE
  16. EXCEPT
  17. FINALLY
  18. RECOVER
  19. BEFORE
  20. EXCEPT: IOError: hi
  21. FINALLY
  22. '''
  23. cmd: "nim c --gc:arc --exceptions:goto $file"
  24. """
  25. #bug 7204
  26. proc nested_finally =
  27. try:
  28. raise newException(KeyError, "msg1")
  29. except KeyError as ex:
  30. echo ex.msg
  31. try:
  32. raise newException(ValueError, "msg2")
  33. except:
  34. echo getCurrentExceptionMsg()
  35. finally:
  36. echo "finally2"
  37. finally:
  38. echo "finally1"
  39. nested_finally()
  40. proc doraise =
  41. raise newException(ValueError, "gah")
  42. proc main =
  43. while true:
  44. try:
  45. echo "begin"
  46. doraise()
  47. finally:
  48. echo "one ", "iteration!"
  49. try:
  50. main()
  51. except:
  52. echo "caught!"
  53. when true:
  54. proc p =
  55. try:
  56. raise newException(Exception, "Hello")
  57. except:
  58. echo "except1"
  59. raise
  60. finally:
  61. echo "finally1"
  62. try:
  63. p()
  64. except:
  65. echo "caught! 2"
  66. proc noException =
  67. try:
  68. echo "BEFORE"
  69. except:
  70. echo "EXCEPT"
  71. raise
  72. finally:
  73. echo "FINALLY"
  74. try: noException()
  75. except: echo "RECOVER"
  76. proc reraise_in_except =
  77. try:
  78. echo "BEFORE"
  79. raise newException(IOError, "")
  80. except IOError:
  81. echo "EXCEPT"
  82. raise
  83. finally:
  84. echo "FINALLY"
  85. try: reraise_in_except()
  86. except: echo "RECOVER"
  87. proc return_in_except =
  88. try:
  89. echo "BEFORE"
  90. raise newException(IOError, "hi")
  91. except:
  92. echo "EXCEPT: ", getCurrentException().name, ": ", getCurrentExceptionMsg()
  93. return
  94. finally:
  95. echo "FINALLY"
  96. try: return_in_except()
  97. except: echo "RECOVER"