Manually set the value of the @error macro (and optionally @extended, and "Return Value").
SetError ( code [, extended = 0 [, return value]] )
code | The required value (integer) to set into the @error macro. |
extended | [optional] (default 0) The required value (integer) to set into the @extended macro. This sets the same macro as the SetExtended() function. |
return value | [optional] (default 1) The value to be returned by the function. |
When entering a user-written function @error macro 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 to be set, then it is recommended to use the SetExtended() function instead.
The return value parameter is optional. It is provided as a way to use the Return SetError(...) syntax to define the value to be returned at the same time as setting @error (and possibly @extended). If a specific value is not set then the return value will set to 1 as SetError() is a successful function.
@error is limited to values between -2147483648 to 2147483647.
#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
#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