parent
a2ad069769
commit
de02fd0b89
2 changed files with 30 additions and 4 deletions
|
|
@ -468,19 +468,36 @@ when defined(endb):
|
||||||
when defined(cpp) and appType != "lib" and
|
when defined(cpp) and appType != "lib" and
|
||||||
not defined(js) and not defined(nimscript) and
|
not defined(js) and not defined(nimscript) and
|
||||||
hostOS != "standalone" and not defined(noCppExceptions):
|
hostOS != "standalone" and not defined(noCppExceptions):
|
||||||
|
|
||||||
|
type
|
||||||
|
StdException {.importcpp: "std::exception", header: "<exception>".} = object
|
||||||
|
|
||||||
|
proc what(ex: StdException): cstring {.importcpp: "((char *)#.what())".}
|
||||||
|
|
||||||
proc setTerminate(handler: proc() {.noconv.})
|
proc setTerminate(handler: proc() {.noconv.})
|
||||||
{.importc: "std::set_terminate", header: "<exception>".}
|
{.importc: "std::set_terminate", header: "<exception>".}
|
||||||
|
|
||||||
setTerminate proc() {.noconv.} =
|
setTerminate proc() {.noconv.} =
|
||||||
# Remove ourself as a handler, reinstalling the default handler.
|
# Remove ourself as a handler, reinstalling the default handler.
|
||||||
setTerminate(nil)
|
setTerminate(nil)
|
||||||
|
|
||||||
|
var msg = "Unknown error in unexpected exception handler"
|
||||||
|
try:
|
||||||
|
raise
|
||||||
|
except Exception:
|
||||||
|
msg = currException.getStackTrace() & "Error: unhandled exception: " &
|
||||||
|
currException.msg & " [" & $currException.name & "]"
|
||||||
|
except StdException as e:
|
||||||
|
msg = "Error: unhandled cpp exception: " & $e.what()
|
||||||
|
except:
|
||||||
|
msg = "Error: unhandled unknown cpp exception"
|
||||||
|
|
||||||
when defined(genode):
|
when defined(genode):
|
||||||
# stderr not available by default, use the LOG session
|
# stderr not available by default, use the LOG session
|
||||||
echo currException.getStackTrace() & "Error: unhandled exception: " &
|
echo msg
|
||||||
currException.msg & " [" & $currException.name & "]\n"
|
|
||||||
else:
|
else:
|
||||||
writeToStdErr currException.getStackTrace() & "Error: unhandled exception: " &
|
writeToStdErr msg & "\n"
|
||||||
currException.msg & " [" & $currException.name & "]\n"
|
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
when not defined(noSignalHandler) and not defined(useNimRtl):
|
when not defined(noSignalHandler) and not defined(useNimRtl):
|
||||||
|
|
|
||||||
9
tests/cpp/tterminate_handler.nim
Normal file
9
tests/cpp/tterminate_handler.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
discard """
|
||||||
|
targets: "cpp"
|
||||||
|
outputsub: "Error: unhandled unknown cpp exception"
|
||||||
|
exitcode: 1
|
||||||
|
"""
|
||||||
|
type Crap {.importcpp: "int".} = object
|
||||||
|
|
||||||
|
var c: Crap
|
||||||
|
raise c
|
||||||
Loading…
Add table
Add a link
Reference in a new issue