tasynctry.nim 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. discard """
  2. output: '''
  3. Generic except: Test
  4. Specific except
  5. Multiple idents in except
  6. Multiple except branches
  7. Multiple except branches 2
  8. '''
  9. targets: "c"
  10. """
  11. import asyncdispatch, strutils
  12. # Here we are testing the ability to catch exceptions.
  13. proc foobar() {.async.} =
  14. if 5 == 5:
  15. raise newException(IndexError, "Test")
  16. proc catch() {.async.} =
  17. # TODO: Create a test for when exceptions are not caught.
  18. try:
  19. await foobar()
  20. except:
  21. echo("Generic except: ", getCurrentExceptionMsg().splitLines[0])
  22. try:
  23. await foobar()
  24. except IndexError:
  25. echo("Specific except")
  26. try:
  27. await foobar()
  28. except OSError, FieldError, IndexError:
  29. echo("Multiple idents in except")
  30. try:
  31. await foobar()
  32. except OSError, FieldError:
  33. assert false
  34. except IndexError:
  35. echo("Multiple except branches")
  36. try:
  37. await foobar()
  38. except IndexError:
  39. echo("Multiple except branches 2")
  40. except OSError, FieldError:
  41. assert false
  42. waitFor catch()
  43. proc test(): Future[bool] {.async.} =
  44. result = false
  45. try:
  46. raise newException(OSError, "Foobar")
  47. except:
  48. result = true
  49. return
  50. proc foo(): Future[bool] {.async.} = discard
  51. proc test2(): Future[bool] {.async.} =
  52. result = false
  53. try:
  54. discard await foo()
  55. raise newException(OSError, "Foobar")
  56. except:
  57. result = true
  58. return
  59. proc test3(): Future[int] {.async.} =
  60. result = 0
  61. try:
  62. try:
  63. discard await foo()
  64. raise newException(OSError, "Hello")
  65. except:
  66. result = 1
  67. raise
  68. except:
  69. result = 2
  70. return
  71. proc test4(): Future[int] {.async.} =
  72. try:
  73. discard await foo()
  74. raise newException(ValueError, "Test4")
  75. except OSError:
  76. result = 1
  77. except:
  78. result = 2
  79. var x = test()
  80. assert x.waitFor()
  81. x = test2()
  82. assert x.waitFor()
  83. var y = test3()
  84. assert y.waitFor() == 2
  85. y = test4()
  86. assert y.waitFor() == 2