tcpp_imported_exc.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. discard """
  2. targets: "cpp"
  3. output: '''caught as std::exception
  4. expected
  5. finally1
  6. finally2
  7. finally2
  8. 2
  9. expected
  10. finally 1
  11. finally 2
  12. expected
  13. cpp exception caught
  14. '''
  15. """
  16. type
  17. std_exception* {.importcpp: "std::exception", header: "<exception>".} = object
  18. std_runtime_error* {.importcpp: "std::runtime_error", header: "<stdexcept>".} = object
  19. std_string* {.importcpp: "std::string", header: "<string>".} = object
  20. proc constructStdString(s: cstring): std_string {.importcpp: "std::string(@)", constructor, header: "<string>".}
  21. proc constructRuntimeError(s: stdstring): std_runtime_error {.importcpp: "std::runtime_error(@)", constructor.}
  22. proc what(ex: std_runtime_error): cstring {.importcpp: "((char *)#.what())".}
  23. proc myexception =
  24. raise constructRuntimeError(constructStdString("cpp_exception"))
  25. try:
  26. myexception() # raise std::runtime_error
  27. except std_exception:
  28. echo "caught as std::exception"
  29. try:
  30. raise constructStdString("x")
  31. except std_exception:
  32. echo "should not happen"
  33. except:
  34. echo "expected"
  35. doAssert(getCurrentException() == nil)
  36. proc earlyReturn =
  37. try:
  38. try:
  39. myexception()
  40. finally:
  41. echo "finally1"
  42. except:
  43. return
  44. finally:
  45. echo "finally2"
  46. earlyReturn()
  47. doAssert(getCurrentException() == nil)
  48. try:
  49. block blk1:
  50. try:
  51. raise newException(ValueError, "mmm")
  52. except:
  53. break blk1
  54. except:
  55. echo "should not happen"
  56. finally:
  57. echo "finally2"
  58. doAssert(getCurrentException() == nil)
  59. #--------------------------------------
  60. # raise by pointer and also generic type
  61. type
  62. std_vector {.importcpp"std::vector", header"<vector>".} [T] = object
  63. proc newVector[T](len: int): ptr std_vector[T] {.importcpp: "new std::vector<'1>(@)".}
  64. proc deleteVector[T](v: ptr std_vector[T]) {.importcpp: "delete @; @ = NIM_NIL;".}
  65. proc len[T](v: std_vector[T]): uint {.importcpp: "size".}
  66. var v = newVector[int](2)
  67. try:
  68. try:
  69. try:
  70. raise v
  71. except ptr std_vector[int] as ex:
  72. echo len(ex[])
  73. raise newException(ValueError, "msg5")
  74. except:
  75. echo "should not happen"
  76. finally:
  77. deleteVector(v)
  78. except:
  79. echo "expected"
  80. doAssert(v == nil)
  81. doAssert(getCurrentException() == nil)
  82. #--------------------------------------
  83. # mix of Nim and imported exceptions
  84. try:
  85. try:
  86. try:
  87. raise newException(KeyError, "msg1")
  88. except KeyError:
  89. raise newException(ValueError, "msg2")
  90. except:
  91. echo "should not happen"
  92. finally:
  93. echo "finally 1"
  94. except:
  95. doAssert(getCurrentExceptionMsg() == "msg2")
  96. raise constructStdString("std::string")
  97. finally:
  98. echo "finally 2"
  99. except:
  100. echo "expected"
  101. doAssert(getCurrentException() == nil)
  102. try:
  103. try:
  104. myexception()
  105. except std_runtime_error as ex:
  106. echo "cpp exception caught"
  107. raise newException(ValueError, "rewritten " & $ex.what())
  108. except:
  109. doAssert(getCurrentExceptionMsg() == "rewritten cpp_exception")
  110. doAssert(getCurrentException() == nil)