Function Reference


SetError

Manually set the value of the @error macro (and optionally @extended, and "Return Value").

SetError ( code [, extended = 0 [, return value]] )

Parameters

code The required value (integer) to set into the @error macro.
extended [optional] The required value (integer) to set into the @extended macro. This sets the same macro as the SetExtended() function.
return value [optional] The value to be returned by the function - if no parameter used the return value is undefined.

Return Value

Returns the "return value" parameter (undefined if the parameter is not used).
@error and @extended will be set to the values set in the function call (both default = 0)

Remarks

When entering a function @error is set to 0. Unless SetError() is called, then @error will remain 0 when the function ends. This means that in order for @error to be set after a function, it must be explicitly set. This also means you may need to backup the status of @error in a variable if you are testing it in a While-WEnd loop.
The extended parameter is optional. It is provided as a way to set both @error and @extended at the same time. If only @extended needs set, then it is recommended to use the SetExtended() function instead.

@error is limited between the values of -2147483648 to 2147483647.

Related

SetExtended

Example

Example 1

#include <MsgBoxConstants.au3>

Local $fResult = myDiv(5, 0)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Error", "Division by Zero")
Else
    MsgBox($MB_SYSTEMMODAL, "Result", $fResult)
EndIf
Exit

Func myDiv($iDividend, $iDivisor)
    If $iDividend = 0 And $iDivisor = 0 Then
        SetError(2) ; Indeterminate form 0/0.
    ElseIf $iDivisor = 0 Then
        SetError(1) ; Plain division by zero.
    EndIf
    Return $iDividend / $iDivisor
EndFunc   ;==>myDiv

Example 2

#include <MsgBoxConstants.au3>

Local $sReturn = Example()

; Display the return value, @error and @extended value.
MsgBox($MB_SYSTEMMODAL, "", "Return value = " & $sReturn & @CRLF & _
        "Value of @error is: " & @error & @CRLF & _
        "Value of @extended is: " & @extended)

Func Example()
    Return SetError(3, 10, "Some return value") ; Set @error to 3, @extended to 10 and return the string "Some return value."
EndFunc   ;==>Example