# OnError() [v1.1.29+]

Specifies a function to run automatically when an unhandled error occurs.

    OnError(Func , AddRemove)

## Parameters

Func

  A function name or function object to call when an unhandled error occurs. The function is given the exception object as a parameter, and may return a non-zero integer to block the default error dialog.

AddRemove

  One of the following values:
  1 (the default): Call the function after any previously registered functions.
  -1: Call the function before any previously registered functions.
  0: Do not call the function.

## Remarks

Func 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 error function returns a non-zero integer, the thread exits. If an error occurs (or an exception is thrown) within an error function, an error message is displayed for the new error and the thread exits. Otherwise, all error function are called, an error message is displayed and the thread exits.

Func's first parameter receives the thrown value or Exception object. If this is an object, it can be modified to affect what the default error dialog displays.

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

## Related

Try, Catch, Throw, OnExit

## Examples

#1

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

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