tcpp_imported_exc.nim 2.9 KB

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