Sign in to follow this  
Followers 0
mwhidden

When is @error set back to 0?

5 posts in this topic

The documentation for SetError() says "When entering a function @error is set to 0.", but it seems there are other circumstances when @error is also set to 0. For example, somewhere in this code, after MakeError() is called, @error gets set back to 0, even though no functions are entered after SetError(1) is called.

Func PassThroughError()
    MakeError()
EndFunc

Func MakeError()
    SetError(1)
EndFunc

PassThroughError()
; Who set @error back to 0?
MsgBox(1, "", "@error is "&@error)
 

Share this post


Link to post
Share on other sites



The @error is only set from the last function that set it from the one that called it.  It's not global throughout all functions.

This may give you a better understanding.

Global $gnError1, $gnError2, $gnError3, $gnError4, $gnError5

Func PassThroughError()
    $gnError1 = @error ; this will zero out @error
    MakeError()
    $gnError4 = @error ; this should zero out, there's no seterror for the return from this function
EndFunc

Func MakeError()
    $gnError2 = @error ; this will zero out @error
    SetError(1)
    $gnError3 = @error ; this is from seterror
EndFunc

PassThroughError()
$gnError5 = @error ; this returns the seterror from PassThroughError only
For $i = 1 To 5
    ConsoleWrite("Error: " & Eval("gnError" & $i) & @CRLF)
Next

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

After reading your response, I thought at first that it was though each function has a local @error, and SetError() reaches up into the calling scope and twiddles the calling function's local @error.

But that's not right because the change of @error is visible within the called function as well as the caller. So I'm guessing instead of setting @error to 0 when a function is entered (as in the docs), what actually happens is that @error is also set to 0 when each function exits, unless SetError was called in the scope of that function itself. Is that right?

Share this post


Link to post
Share on other sites

Yes


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0