diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2017-07-12 15:37:41 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2017-07-12 15:37:49 +0200 |
commit | 57ed077c1eefa0e1818b36525fdc238160b4418c (patch) | |
tree | 00f2f48083b07517663f98590e2d4acde6cc0d5e | |
parent | ccbc09fb0bd82e0ee0db8293cded129b48fa48ee (diff) | |
download | Nim-57ed077c1eefa0e1818b36525fdc238160b4418c.tar.gz |
add system.onUnhandledException feature
-rw-r--r-- | lib/system/excpt.nim | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/lib/system/excpt.nim b/lib/system/excpt.nim index 096d01845..cfb6492b2 100644 --- a/lib/system/excpt.nim +++ b/lib/system/excpt.nim @@ -230,6 +230,18 @@ when false: pushCurrentException(e) c_longjmp(excHandler.context, 1) +var onUnhandledException*: (proc (errorMsg: string) {. + nimcall.}) ## set this error \ + ## handler to override the existing behaviour on an unhandled exception. + ## The default is to write a stacktrace to ``stderr`` and then call ``quit(1)``. + ## Unstable API. + +template unhandled(buf, body) = + if onUnhandledException != nil: + onUnhandledException(buf) + else: + body + proc raiseExceptionAux(e: ref Exception) = if localRaiseHook != nil: if not localRaiseHook(e): return @@ -260,7 +272,9 @@ proc raiseExceptionAux(e: ref Exception) = add(buf, " [") add(buf, $e.name) add(buf, "]\n") - showErrorMessage(buf) + unhandled(buf): + showErrorMessage(buf) + quitOrDebug() else: # ugly, but avoids heap allocations :-) template xadd(buf, s, slen: expr) = @@ -276,8 +290,9 @@ proc raiseExceptionAux(e: ref Exception) = add(buf, " [") xadd(buf, e.name, e.name.len) add(buf, "]\n") - showErrorMessage(buf) - quitOrDebug() + unhandled(buf): + showErrorMessage(buf) + quitOrDebug() proc raiseException(e: ref Exception, ename: cstring) {.compilerRtl.} = if e.name.isNil: e.name = ename |