texceptions2.nim 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. discard """
  2. disabled: "posix" # already covered by texceptions.nim
  3. matrix: "-d:nimStdSetjmp; -d:nimRawSetjmp; -d:nimBuiltinSetjmp"
  4. output: '''
  5. BEFORE
  6. FINALLY
  7. BEFORE
  8. EXCEPT
  9. FINALLY
  10. RECOVER
  11. BEFORE
  12. EXCEPT: IOError: hi
  13. FINALLY
  14. '''
  15. """
  16. echo ""
  17. proc no_exception =
  18. try:
  19. echo "BEFORE"
  20. except:
  21. echo "EXCEPT"
  22. raise
  23. finally:
  24. echo "FINALLY"
  25. try: no_exception()
  26. except: echo "RECOVER"
  27. echo ""
  28. proc reraise_in_except =
  29. try:
  30. echo "BEFORE"
  31. raise newException(IOError, "")
  32. except IOError:
  33. echo "EXCEPT"
  34. raise
  35. finally:
  36. echo "FINALLY"
  37. try: reraise_in_except()
  38. except: echo "RECOVER"
  39. echo ""
  40. proc return_in_except =
  41. try:
  42. echo "BEFORE"
  43. raise newException(IOError, "hi")
  44. except:
  45. echo "EXCEPT: ", getCurrentException().name, ": ", getCurrentExceptionMsg()
  46. return
  47. finally:
  48. echo "FINALLY"
  49. try: return_in_except()
  50. except: echo "RECOVER"
  51. block: #10417
  52. proc moo() {.noreturn.} = discard
  53. let bar =
  54. try:
  55. 1
  56. except:
  57. moo()
  58. doAssert(bar == 1)
  59. # Make sure the VM handles the exceptions correctly
  60. block:
  61. proc fun1(): seq[int] =
  62. try:
  63. try:
  64. raise newException(ValueError, "xx")
  65. except:
  66. doAssert("xx" == getCurrentExceptionMsg())
  67. raise newException(KeyError, "yy")
  68. except:
  69. doAssert("yy" == getCurrentExceptionMsg())
  70. result.add(1212)
  71. try:
  72. try:
  73. raise newException(AssertionDefect, "a")
  74. finally:
  75. result.add(42)
  76. except AssertionDefect:
  77. result.add(99)
  78. finally:
  79. result.add(10)
  80. result.add(4)
  81. result.add(0)
  82. try:
  83. result.add(1)
  84. except KeyError:
  85. result.add(-1)
  86. except ValueError:
  87. result.add(-1)
  88. except IndexDefect:
  89. result.add(2)
  90. except:
  91. result.add(3)
  92. try:
  93. try:
  94. result.add(1)
  95. return
  96. except:
  97. result.add(-1)
  98. finally:
  99. result.add(2)
  100. except KeyError:
  101. doAssert(false)
  102. finally:
  103. result.add(3)
  104. let x1 = fun1()
  105. const x2 = fun1()
  106. doAssert(x1 == x2)