I change lib/system/excpt.nim(362)
quit(1)
to
raise newException(SystemError, "SystemError")
and the following codes works
var a: int = 0
try:
var b: int = 5 div a
echo b
except SystemError:
echo "Exception: " & getCurrentExceptionMsg()
echo "end"
outputs:
SIGFPE: Arithmetic error.
Exception: SystemError
end
From excpt's source code, it was written with systems without exceptions in mind: Exception handling code. Carefully coded so that tiny programs which do not use the heap (and nor exceptions) do not include the GC or memory allocator.
You can set a custom message however:
errorMessageWriter = proc(m: string) = echo "Custom: ", m
var i = 0
echo 5 div i
# Custom: Traceback (most recent call last)
# main.nim(5) main
#system.nim(2534) sysFatal
# Error: unhandled exception: division by zero [DivByZeroError]
It depends on what you need, you probably prefer to set a global/local exception handler:
globalRaiseHook = proc (e: ref Exception): bool =
echo "AY AY AY!"
quit(1)
var i = 0
echo 5 div i
# AY AY AY!
# exit status 1