texceptions.nim 1.9 KB

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