Quirrel4.41.0

debug.seterrorhandler

Defined in module "debug"
from "debug" import seterrorhandler
seterrorhandler(handler: function|null)

Installs the given function as the VM error handler; null clears it

examples/debug/seterrorhandler-basic.nut
from "debug" import seterrorhandler

seterrorhandler(function(err) {
  println("handler saw:", err)
})

// the error still propagates to the caller as a normal exception
let worker = newthread(function() { throw "ammo depleted" })
try { worker.call() } catch (e) { println("caller also caught:", e) }
Output:
handler saw: ammo depleted
caller also caught: ammo depleted

Parameters

handlerfunction|nullcalled as handler(error), or handler(error, trace) when it takes three parameters or a trailing ...; null clears the handler

Notes

The handler runs when an error unwinds all the way past the last script call on the stack, uncaught by any try/catch in between. It is a notification, not a replacement for catch: after the handler returns, the error still propagates to whatever native code made that call, such as a thread.call() from another part of the script, as if no handler had run.

trace is null unless the caller that raised the error supplied one.

Example

examples/debug/seterrorhandler.nut
from "debug" import seterrorhandler

seterrorhandler(function(err) {
  println("handler saw:", err)
})

// the error reaches the handler only once it is uncaught all the way up to
// this call, then still propagates to us as a normal exception
let co = newthread(function() { throw "boom" })
try { co.call() } catch (e) { println("caller also caught:", e) }

seterrorhandler(null)
let co2 = newthread(function() { throw "boom2" })
try { co2.call() } catch (e) { println("no handler this time:", e) }
Output:
handler saw: boom
caller also caught: boom
no handler this time: boom2

See also

setdebughookInstalls the given function as the VM debug hook; null clears it
debugmodule index