OnError() [v1.1.29+]

Registers a function to be called automatically whenever an unhandled error occurs.

OnError(Callback , AddRemove)

Parameters

Callback

A function name or function object to call. To pass a literal function name, enclose it in quotes.

The callback accepts one parameter and can be defined as follows:

MyCallback(Exception) { ...

Although the name you give the parameter does not matter, it is assigned the thrown value or Exception object. If this is an object, it can be modified to affect what the default error dialog displays.

You can omit the callback's parameter if the corresponding information is not needed.

The callback can return a non-zero integer to block the default error dialog.

AddRemove

If omitted, it defaults to 1. Otherwise, specify one of the following numbers:

Remarks

Callback is called only for errors or exceptions which would normally cause an error message to be displayed. It cannot be called for a load-time error, since OnError cannot be called until after the script has loaded.

If any callback returns a non-zero integer, the thread exits. If an error occurs (or an exception is thrown) within a callback, an error message is displayed for the new error and the thread exits. Otherwise, all callbacks are called, an error message is displayed and the thread exits.

Callback is called on the current thread, before it exits (that is, before the call stack unwinds).

Try, Catch, Throw, OnExit

Examples

Logs errors caused by the script into a text file instead of displaying them to the user.

OnError("LogError")
%cause% := error

LogError(exc) {
    FileAppend % "Error on line " exc.Line ": " exc.Message "`n"
        , errorlog.txt
    return true
}

Same as above but with a function object.

OnError(LogError)
%cause% := error

class LogError {
    Call(exc) {
        FileAppend % "Error on line " exc.Line ": " exc.Message "`n"
            , errorlog.txt
        return true
    }
}