Jump to content

@error lost on Return without SetError?


Recommended Posts

44 minutes ago, argumentum said:

..early morning for me but this looks like a collective brain fog. The ConsoleWrite() behaves as:

Func ConsoleWrite_BehavesAs($data, $iError = @error, $iExtended = @extended)
    Local $iLength = ConsoleWrite($data)
    Return SetError($iError, $iExtended, $iLength)
EndFunc

I don't see anything wrong or confusing on any of the above posts. I don't find anything perplexing in the OP question.

You are not awake yet. :lol:

Edited by Andreik
Link to comment
Share on other sites

From my POV, the issue occurs whenever SetError isn't the last executed statement prior to returning from the function. It's like AutoIt detects that @error was set by something other than SetError (it was originally set by SetError, but then reset by ConsoleWrite), and therefore doesn't pass these error values to the calling routine.

Link to comment
Share on other sites

52 minutes ago, Andreik said:

You are not awake yet. :lol:

lol, I was not :D
 

FuncTest()
ConsoleWrite( "After FuncTest Call.   @error is " & @error & @CRLF )

Func FuncTest()
    ConsoleWrite("AutoItVersion = v" & @AutoItVersion & @CRLF)
    SetError(2) ; <--- this does not behave as expected
    ConsoleWrite('FuncTest: inner check: ' & @error & @CRLF) ; this behaves as expected in version 3.3.16.1
    ConsoleWrite('FuncTest: inner check: ' & @error & @CRLF)
    ConsoleWrite('FuncTest: inner check: ' & @error & @CRLF) ;   but not in version 3.3.14.5 ( irrelevant )
    Return ; SetError(2) ; <--- it should be the same as if placed here, but is not.
EndFunc   ;==>FuncTest

yes, the SetError() is not behaving as expected. I call BS bug !  :)

Edit: The way it works lead me to misinterpret it.

Edited by argumentum
oops

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Developers
1 hour ago, Andreik said:

@Jos I think they refer to what help file states for ConsoleWrite().

If I understand right ConsoleWrite() should not affect @error macro.

True...    so the @error only survives the Func call when it contains the SetError() statement inside or else it is lost when returning.

Main()
Func Main()
    FuncS(1)
    ConsoleWrite("FuncS=1.   @error = " & @error & @CRLF)
    FuncS(2)
    ConsoleWrite("FuncS=2 .   @error = " & @error & @CRLF)
EndFunc   ;==>Main

Func FuncS($i)
    RegRead("XXX", "")
    ConsoleWrite(@error & @CRLF)
    ConsoleWrite(@error & @CRLF)
    If $i = 2 Then SetError(@error)
    Return
EndFunc   ;==>FuncS

 

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

@Jos What about this modified version of your code? Should @error still be returned from the function when $i = 2?

Main()
Func Main()
    FuncS(1)
    ConsoleWrite("FuncS=1.   @error = " & @error & @CRLF)
    FuncS(2)
    ConsoleWrite("FuncS=2 .   @error = " & @error & @CRLF)
EndFunc   ;==>Main

Func FuncS($i)
    RegRead("XXX", "")
    ConsoleWrite(@error & @CRLF)
    ConsoleWrite(@error & @CRLF)
    If $i = 2 Then 
        SetError(@error)
        ConsoleWrite(@error & @CRLF)
    EndIf
    Return
EndFunc   ;==>FuncS

 

Link to comment
Share on other sites

7 minutes ago, Jos said:

True...    so the @error only survives the Func call when it contains the SetError() statement inside or else it is lost when returning.

It's not enough just to be set but it is also important to be the last statement before return.

Link to comment
Share on other sites

14 minutes ago, Andreik said:

It's not enough just to be set but it is also important to be the last statement before return.

..I was reading the manual because, I never do.
"Manually set the value of the @error macro (and optionally @extended, and "Return Value")."

So the behavior is not as I thought the OP believed it should (I never use it in this way but, in that trend of thought made sense)
But I stand corrected. SetError() sets the @error but does not avoid other @error to be set.
The confusion is because if used it will return the @error on return without having to declare at return. That in itself is nice but leads to confusion.

Edited by argumentum

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

  • Developers
30 minutes ago, Danp2 said:

@Jos What about this modified version of your code? Should @error still be returned from the function when $i = 2?

Yea, it isn't the last statement, so just turn them around:

Main()
Func Main()
    FuncS(1)
    ConsoleWrite("FuncS=1.   @error = " & @error & @CRLF)
    FuncS(2)
    ConsoleWrite("FuncS=2 .   @error = " & @error & @CRLF)
EndFunc   ;==>Main

Func FuncS($i)
    RegRead("XXX", "")
    ConsoleWrite(@error & @CRLF)
    ConsoleWrite(@error & @CRLF)
    If $i = 2 Then
        ConsoleWrite(@error & @CRLF)
        SetError(@error)
    EndIf
    Return
EndFunc   ;==>FuncS

.. and yes I know there is only an ConsoleWrite after it but honestly is the change made by JPM in 2020 nice but confusing to me!

So to me this is the right approach: Make sure you take control over your script and capture @error right after an performed Func when you need it later!

Edited by Jos

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

12 minutes ago, Jos said:

So to me this is the right approach: Make sure you take control over your script and capture @error right after an performed Func when you need it later!

Totally agree. ConsoleWrite() shouldn't behave different than any other function. If you want a certain error code later in your code, make sure you save it. This exception does not encourage good programming practices at all. I won't even talk about those who debug their code using other functions like MsgBox(), _ArrayDisplay(), etc. Will there be exceptions for these functions also?

Edited by Andreik
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

×
×
  • Create New...