tgotoexceptions2.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. discard """
  2. cmd: "nim c --gc:arc --exceptions:goto $file"
  3. output: '''
  4. B1
  5. B2
  6. catch
  7. A1
  8. 1
  9. B1
  10. B2
  11. catch
  12. A1
  13. A2
  14. 0
  15. B1
  16. B2
  17. A1
  18. 1
  19. B1
  20. B2
  21. A1
  22. A2
  23. 3
  24. A
  25. B
  26. C
  27. '''
  28. """
  29. # More thorough test of return-in-finaly
  30. var raiseEx = true
  31. var returnA = true
  32. var returnB = false
  33. proc main: int =
  34. try: #A
  35. try: #B
  36. if raiseEx:
  37. raise newException(OSError, "")
  38. return 3
  39. finally: #B
  40. echo "B1"
  41. if returnB:
  42. return 2
  43. echo "B2"
  44. except OSError: #A
  45. echo "catch"
  46. finally: #A
  47. echo "A1"
  48. if returnA:
  49. return 1
  50. echo "A2"
  51. for x in [true, false]:
  52. for y in [true, false]:
  53. # echo "raiseEx: " & $x
  54. # echo "returnA: " & $y
  55. # echo "returnB: " & $z
  56. # in the original test returnB was set to true too and
  57. # this leads to swallowing the OSError exception. This is
  58. # somewhat compatible with Python but it's non-sense, 'finally'
  59. # should not be allowed to swallow exceptions. The goto based
  60. # implementation does something sane so we don't "correct" its
  61. # behavior just to be compatible with v1.
  62. raiseEx = x
  63. returnA = y
  64. echo main()
  65. # Various tests of return nested in double try/except statements
  66. proc test1() =
  67. defer: echo "A"
  68. try:
  69. raise newException(OSError, "Problem")
  70. except OSError:
  71. return
  72. test1()
  73. proc test2() =
  74. defer: echo "B"
  75. try:
  76. return
  77. except OSError:
  78. discard
  79. test2()
  80. proc test3() =
  81. try:
  82. try:
  83. raise newException(OSError, "Problem")
  84. except OSError:
  85. return
  86. finally:
  87. echo "C"
  88. test3()