Jump to content

@error Not Returning Correctly


Recommended Posts

Hi, I've noticed just recently that alot of my code breaks because of a problem with AutoIT's error return code. Try the following example:

Func _ReturnErr()
    Return SetError(1)
EndFunc

Func _ReturnReturnErr()
    Return _ReturnErr()
EndFunc

_ReturnReturnErr()
If @error Then ConsoleWrite("Error returned correctly"&@CRLF)

Notice how it never writes anything to the console? Why is that? Shouldn't this work just as a 'Return SetError(1)' should? It doesn't make sense why the @error is cleared, when it should really return the correct @error code.

If this is documented somewhere let me know please. Otherwise I will report it as a bug. This isn't acceptable behavior to me.

Thanks,

Ascend4nt

*edit: fyi, I'm running AutoIT v3.3

Edited by ascendant
Link to comment
Share on other sites

  • Developers

Pretty sure that the @error is reset for each function:

Remarks

When entering a function @error is set to 0. Unless SetError() is called, then @error will remain 0 after the function has ended. 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 only 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.

This means you need to set the error again in _ReturnReturnErr():

Func _ReturnErr()
    Return SetError(1)
EndFunc  ;==>_ReturnErr
Func _ReturnReturnErr()
    Return SetError(_ReturnErr())
EndFunc  ;==>_ReturnReturnErr
_ReturnReturnErr()
If @error Then ConsoleWrite("Error returned correctly" & @CRLF)

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Moderators

Not sure if localization has anything to do with it, however you can remedy the situation:

Func _ReturnErr()
    Return SetError(1, 0, 0)
EndFunc

Func _ReturnReturnErr()
    _ReturnErr()
    If @error Then SetError(@error)
    Return
EndFunc

_ReturnReturnErr()
If @error Then ConsoleWrite("Error returned correctly"&@CRLF)

Edit:

Or what Jos said, follows my thoughts on it anyway (The localization).

Edited by SmOke_N

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.

Link to comment
Share on other sites

@Jos, Return SetError(_ReturnErr()) won't work for functions where the function being called is used for a return value, unless you call it twice, which is pointless (like this:)

Return SetError(_ReturnErr(),0,_ReturnErr())

@SmOke_N, that is what I've had to do - but look how foolish it looks, when you are setting @error to @error. This is a dumb (though working) workaround - not that I mean dumb as in your implementation, but the fact that this is needed at all - and anyone looking at the code will be 'wth?!'.

I think this needs to be requested - that if a function is using 'Return functionname()', that 'functionname()' should both set @error and return the value.

Thanks guys for the help,

Ascend4nt

*note: I just realized the above code example would be foolish for a function that returns a value - you'll be setting @error to the return value haha :P

Edited by ascendant
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...