tcpp_imported_exc.nim 2.9 KB

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