summary refs log tree commit diff stats
path: root/lib/system/excpt.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/system/excpt.nim')
-rw-r--r--lib/system/excpt.nim18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim
index 9d33db4cb..dbdd038a4 100644
--- a/lib/system/excpt.nim
+++ b/lib/system/excpt.nim
@@ -469,6 +469,24 @@ when defined(endb):
   var
     dbgAborting: bool # whether the debugger wants to abort
 
+when defined(cpp) and appType != "lib" and
+    not defined(js) and not defined(nimscript) and
+    hostOS != "standalone" and not defined(noCppExceptions):
+  proc setTerminate(handler: proc() {.noconv.})
+    {.importc: "std::set_terminate", header: "<exception>".}
+  setTerminate proc() {.noconv.} =
+    # Remove ourself as a handler, reinstalling the default handler.
+    setTerminate(nil)
+
+    when defined(genode):
+      # stderr not available by default, use the LOG session
+      echo currException.getStackTrace() & "Error: unhandled exception: " &
+              currException.msg & " [" & $currException.name & "]\n"
+    else:
+      writeToStdErr currException.getStackTrace() & "Error: unhandled exception: " &
+              currException.msg & " [" & $currException.name & "]\n"
+    quit 1
+
 when not defined(noSignalHandler) and not defined(useNimRtl):
   proc signalHandler(sign: cint) {.exportc: "signalHandler", noconv.} =
     template processSignal(s, action: untyped) {.dirty.} =