Jump to content

Recommended Posts

Posted

The Help file is very explicit about @error being reset to zero on entry to a User Function. But it is silent (as far as I can tell) about @error on Return from a Function. So I don't understand what is happening in this code.

Why does FuncR( ) return @error = 0?

Why does "SetError( @error )" in FuncS make it work the way I expected? As I read it, "SetError( @error )" says "Set @error equal to @error". Which would seem to be a NoOp.

Main()
Func Main()
    ConsoleWrite( "@AutoItVersion = " & @AutoItVersion & @CRLF )
    RegRead( "XXX", "" )
    ConsoleWrite( "RegRead. @error = " & @error & @CRLF )
    FuncR( )
    ConsoleWrite( "FuncR.   @error = " & @error & @CRLF )
    FuncS( )
    ConsoleWrite( "FuncS.   @error = " & @error & @CRLF )
EndFunc

Func FuncR( )
    RegRead( "XXX", "" )
EndFunc

Func FuncS( )
    RegRead( "XXX", "" )
    SetError( @error )
EndFunc

-- Output ---------------------------------------------------------------------
@AutoItVersion = 3.3.16.1
RegRead. @error = 2
FuncR.   @error = 0
FuncS.   @error = 2

 

Posted

so he has a different attitude

Main()
Func Main()
    ConsoleWrite( "@AutoItVersion = " & @AutoItVersion & @CRLF )
    RegRead( "XXX", "" )
    ConsoleWrite( "RegRead. @error = " & @error & @CRLF )

    FuncR( )
    FuncS( )

EndFunc

Func FuncR( )
    RegRead( "XXX", "" )
    ConsoleWrite( "FuncR.   @error = " & @error & @CRLF )
EndFunc

Func FuncS( )
    RegRead( "XXX", "" )
    SetError( @error )
    ConsoleWrite( "FuncS.   @error = " & @error & @CRLF )
EndFunc

 

I know that I know nothing

Posted

@AGlassman I seem to recall experiencing the same issue when I first started working on a UDF. I think this entry from the Func help file entry applies --

  Quote

Using Return with SetError() allows @error and @extended values to be returned as well as a value.

Expand  

@ioa747 Pretty sure your calls to ConsoleWrite within each function will wipe out the value of the @error codes.

Posted

@AGlassman, please do not rely on the engine to handle such things for you. if you need the function to return a specific @error value (or @extended, or return value) please put yourself in a habit to set it explicitly. it will save you a lot of trouble.

in more ancient programming languages, when you declared a variable, you had to initialize it to zero, or it might contain some random value. this i find still to be a solid advice.

and the help file does clearly state, "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. "

 

 

Signature - my forum contributions:

  Reveal hidden contents

 

Posted (edited)
  On 11/2/2023 at 8:50 PM, Danp2 said:

calls to ConsoleWrite within each function will wipe out the value of the @error codes.

Expand  

Recent AutoIt version preservs errors and extendent when you call ConsoleWrite

EDIT as far as I remember....

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 11/2/2023 at 7:53 PM, AGlassman said:
Func FuncS( )
    RegRead( "XXX", "" )
    SetError( @error )
EndFunc
Expand  

This is wrong usage ... of course if you wana return error from this function.

...

Return SetError(....

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted
  On 11/2/2023 at 8:50 PM, Danp2 said:

@AGlassman I seem to recall experiencing the same issue when I first started working on a UDF. I think this entry from the Func help file entry applies --

@ioa747 Pretty sure your calls to ConsoleWrite within each function will wipe out the value of the @error codes.

Expand  

Danp2 - I'm not sure I understand how your Help file reference applies in this case. I am not concerned with @extended or the returned value. Only @error.

ioa747. It would seem that ConsoleWrite is NOT reseting @error. It would appear that @error only gets reset to zero after returning from FuncR.

Main()
Func Main()
    ConsoleWrite( "@AutoItVersion = " & @AutoItVersion & @CRLF )
    RegRead( "XXX", "" )
    ConsoleWrite( "RegRead.           @error = " & @error & @CRLF )

    FuncR( )
    ConsoleWrite( "FuncR Return.      @error = " & @error & @CRLF )

    FuncS( )
    ConsoleWrite( "FuncS Return.      @error = " & @error & @CRLF )

EndFunc

Func FuncR( )
    RegRead( "XXX", "" )
    ConsoleWrite( "FuncR 1st Write.   @error = " & @error & @CRLF )
    ConsoleWrite( "FuncR 2nd Write.   @error = " & @error & @CRLF )
EndFunc

Func FuncS( )
    RegRead( "XXX", "" )
    SetError( @error )
    ConsoleWrite( "FuncS.             @error = " & @error & @CRLF )
EndFunc

-- Output ---------------------------------------------------------------------
@AutoItVersion = 3.3.16.1
RegRead.           @error = 2
FuncR 1st Write.   @error = 2
FuncR 2nd Write.   @error = 2
FuncR Return.      @error = 0
FuncS.             @error = 2
FuncS Return.      @error = 0

 

Posted
  On 11/2/2023 at 9:52 PM, Nine said:

Not anymore, see last update notes...

Expand  

Maybe we've hit an edge case where it is still not working correctly. Here's the code I'm using to test --

Main()
Func Main()
    ConsoleWrite( "@AutoItVersion = " & @AutoItVersion & @CRLF )
    RegRead( "XXX", "" )
    ConsoleWrite( "RegRead. @error = " & @error & @CRLF )

    FuncR( )
    ;### Debug CONSOLE ↓↓↓
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : @error = ' & @error & @CRLF & '>Error code: ' & @error & @CRLF)
    FuncS( )
    ;### Debug CONSOLE ↓↓↓
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : @error = ' & @error & @CRLF & '>Error code: ' & @error & @CRLF)

EndFunc

Func FuncR( )
    RegRead( "XXX", "" )
    ConsoleWrite( "FuncR.   @error = " & @error & @CRLF )
EndFunc

Func FuncS( )
    RegRead( "XXX", "" )
    SetError( @error )
    ConsoleWrite( "FuncS.   @error = " & @error & @CRLF )
EndFunc

Here's the output when the ConsoleWrite is present in FuncS --

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\danpo\OneDrive\Documents\GitHub\WD Test\test3.au3" /UserParams    
+>17:03:16 Starting AutoIt3Wrapper (23.402.1150.2) from:SciTE.exe (5.3.8.0)  Keyboard:00000409  OS:WIN_11/2009  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\danpo\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\danpo\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.16.1)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\danpo\OneDrive\Documents\GitHub\WD Test\test3.au3
+>17:03:16 AU3Check ended. rc:0
>Running:(3.3.16.1):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "C:\Users\danpo\OneDrive\Documents\GitHub\WD Test\test3.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart. --> Press Ctrl+BREAK to Stop.
@AutoItVersion = 3.3.16.1
RegRead. @error = 2
FuncR.   @error = 2
@@ Debug(9) : @error = 0
>Error code: 0
FuncS.   @error = 2
@@ Debug(12) : @error = 0
>Error code: 0
+>17:03:16 AutoIt3 ended. rc:0
+>17:03:16 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 0.5162

This is the output when that line is removed --

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Users\danpo\OneDrive\Documents\GitHub\WD Test\test3.au3" /UserParams    
+>17:05:49 Starting AutoIt3Wrapper (23.402.1150.2) from:SciTE.exe (5.3.8.0)  Keyboard:00000409  OS:WIN_11/2009  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\danpo\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\danpo\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.16.1)  from:C:\Program Files (x86)\AutoIt3  input:C:\Users\danpo\OneDrive\Documents\GitHub\WD Test\test3.au3
+>17:05:49 AU3Check ended. rc:0
>Running:(3.3.16.1):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "C:\Users\danpo\OneDrive\Documents\GitHub\WD Test\test3.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart. --> Press Ctrl+BREAK to Stop.
@AutoItVersion = 3.3.16.1
RegRead. @error = 2
FuncR.   @error = 2
@@ Debug(9) : @error = 0
>Error code: 0
@@ Debug(12) : @error = 2
>Error code: 2
+>17:05:49 AutoIt3 ended. rc:0
+>17:05:49 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 0.6063

 

Posted
  On 11/2/2023 at 10:06 PM, Danp2 said:

Maybe we've hit an edge case where it [ConsoleWrite] is still not working correctly.

Expand  

Danp2 - It does not appear to have anything to do with calling ConsoleWrite (see code below).

Inside FuncR @error is non-zero. But on return from the function it has been reset to zero.

Which I could accept as "That's just how it works. Return resets @error. Add it to the AutoIt Gotcha! list".

Except that doing a "SetError", instead of calling code that sets @error (see FuncT), seems to have some sort of side effect that makes the @error value "stick" on return.

Main()
Func Main()

    ConsoleWrite( "@AutoItVersion = " & @AutoItVersion & @CRLF )

    RegRead( "XXX", "" )
    If Not @error Then ConsoleWrite( "After RegRead.        @error is Zero" & @CRLF )

    FuncR( )
    If Not @error Then ConsoleWrite( "After FuncR Call.     @error is Zero" & @CRLF )

    FuncT( )
    If Not @error Then ConsoleWrite( "After FuncT Call.     @error is Zero" & @CRLF )

EndFunc

Func FuncR( )

    RegRead( "XXX", "" )
    If Not @error Then ConsoleWrite( "Before FuncR Return.  @error is Zero" & @CRLF )
    Return

EndFunc

Func FuncT( )

    SetError( 2 )
    If Not @error Then ConsoleWrite( "Before FuncT Return.  @error is Zero" & @CRLF )
    Return

EndFunc

---  Output ---------------------------------------------------------------------
@AutoItVersion = 3.3.16.1
After FuncR Call.     @error is Zero
+>21:01:35 AutoIt3.exe ended.rc:0

 

Posted
  On 11/3/2023 at 2:03 AM, AGlassman said:

Danp2 - It does not appear to have anything to do with calling ConsoleWrite (see code below).

Expand  

I disagree. Your code as posted doesn't call ConsoleWrite within the function because @error is always not zero. Try it with this modified version --

Main()
Func Main()

    ConsoleWrite( "@AutoItVersion = " & @AutoItVersion & @CRLF )

    RegRead( "XXX", "" )
    If Not @error Then ConsoleWrite( "After RegRead.        @error is Zero" & @CRLF )

    FuncR( )
    If Not @error Then ConsoleWrite( "After FuncR Call.     @error is Zero" & @CRLF )

    FuncT( )
    If Not @error Then ConsoleWrite( "After FuncT Call.     @error is Zero" & @CRLF )

EndFunc

Func FuncR( )

    RegRead( "XXX", "" )
    If @error Then ConsoleWrite( "Before FuncR Return.  @error is non-Zero" & @CRLF )
    Return

EndFunc

Func FuncT( )

    SetError( 2 )
    If @error Then ConsoleWrite( "Before FuncT Return.  @error is non-Zero" & @CRLF )
    Return

EndFunc

---  Output ---------------------------------------------------------------------
Before FuncR Return.  @error is non-Zero
After FuncR Call.     @error is Zero
Before FuncT Return.  @error is non-Zero
After FuncT Call.     @error is Zero

 

Posted

As to your statement:

  On 11/2/2023 at 7:53 PM, AGlassman said:

Why does "SetError( @error )" in FuncS make it work the way I expected? As I read it, "SetError( @error )" says "Set @error equal to @error". Which would seem to be a NoOp.

Expand  

Here I wrote:

  On 11/2/2023 at 8:56 PM, mLipok said:

This is wrong usage ... of course if you wana return error from this function.

...

Return SetError(....

Expand  

And this is what I mean:

Main()
Func Main()
    ConsoleWrite("@AutoItVersion = " & @AutoItVersion & @CRLF)
    RegRead("XXX", "")
    ConsoleWrite("RegRead. @error = " & @error & @CRLF)
    FuncR()
    ConsoleWrite("FuncR.   @error = " & @error & @CRLF)
    FuncS()
    ConsoleWrite("FuncS.   @error = " & @error & @CRLF)
    FuncS2()
    ConsoleWrite("FuncS2.   @error = " & @error & @CRLF)
    FuncS3()
    ConsoleWrite("FuncS3.   @error = " & @error & @CRLF)
    FuncS4_1()
    ConsoleWrite("FuncS4_1.   @error = " & @error & @CRLF)
    FuncS4_2()
    ConsoleWrite("FuncS4_2.   @error = " & @error & @CRLF)
    FuncS5_1()
    ConsoleWrite("FuncS5_1.   @error = " & @error & @CRLF)
    FuncS5_2()
    ConsoleWrite("FuncS5_2.   @error = " & @error & @CRLF)
EndFunc   ;==>Main

Func FuncR()
    RegRead("XXX", "")
EndFunc   ;==>FuncR

Func FuncS()
    RegRead("XXX", "")
    SetError(@error)
EndFunc   ;==>FuncS

Func FuncS2()
    RegRead("XXX", "")
    SetError(@error)
EndFunc   ;==>Func2

Func FuncS3()
    RegRead("XXX", "")
    SetError(@error)
    Return
EndFunc   ;==>FuncS3

Func FuncS4_1()
    RegRead("XXX", "")
    ConsoleWrite(@CRLF)
    Return
EndFunc   ;==>FuncS3

Func FuncS4_2()
    RegRead("XXX", "")
    SetError(@error)
    ConsoleWrite(@CRLF)
    Return
EndFunc   ;==>FuncS3

Func FuncS5_1()
    RegRead("XXX", "")
    ConsoleWrite(@CRLF)
    Return SetError(@error)
EndFunc   ;==>FuncS3

Func FuncS5_2()
    RegRead("XXX", "")
    SetError(@error)
    ConsoleWrite(@CRLF)
    Return SetError(@error)
EndFunc   ;==>FuncS3

In my understanding only FuncS3, FuncS5_1, FuncS5_2 should be correct way how to return desired error from function.

 

But I also wonder why FuncS2 and FuncS3 returns errors in relation to the fact that FuncS4_1 and FuncS4_2 does not retur error

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Hope @jpm will take a look here soon.

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

  • Developers
Posted (edited)
  On 11/3/2023 at 1:48 AM, Nine said:

Agree, very edgy.  And also very ugly.  :ermm:

But we may be able to call it a bug... 

Expand  

Looks good to me...   @error contains the last func's value.

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.
  :)

  • Developers
Posted (edited)
  On 11/3/2023 at 9:55 AM, mLipok said:

ut I also wonder why FuncS2 and FuncS3 returns errors in relation to the fact that FuncS4_1 and FuncS4_2 does not retur error

Expand  

again...  all making sense! 

FuncS4_1 & 2 return the @error value of consolewrite, so logically 0 

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.
  :)

Posted

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

  Quote

The @error and @extended are not set on return leaving them as they were before calling. Usefull when debugging with the SciTE debugging output.

Expand  

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

Posted

..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.

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

Posted

@Jos I was refering to the same what @Andreik mention (thanks).

@argumentum yes it should behaves like you said 'The ConsoleWrite() behaves as:'

but why it is not working this way in  FuncS4_1 and FuncS4_2  ?

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)
  On 11/3/2023 at 12:58 PM, mLipok said:

but why it is not working this way in  FuncS4_1 and FuncS4_2  ?

Expand  

to better illustrate the problem:

;~ https://www.autoitscript.com/forum/topic/211040-error-lost-on-return-without-seterror/#comment-1526182

Main()
Func Main()
    ConsoleWrite("@AutoItVersion = " & @AutoItVersion & @CRLF)
    RegRead("XXX", "")
    ConsoleWrite("RegRead. @error = " & @error & @CRLF)
    FuncR()
    ConsoleWrite("FuncR.   @error = " & @error & @CRLF)
    FuncS()
    ConsoleWrite("FuncS.   @error = " & @error & @CRLF)
    FuncS2()
    ConsoleWrite("FuncS2.   @error = " & @error & @CRLF)
    FuncS3()
    ConsoleWrite("FuncS3.   @error = " & @error & @CRLF)
    FuncS4_1()
    ConsoleWrite("FuncS4_1.   @error = " & @error & @CRLF)
    FuncS4_2()
    ConsoleWrite("FuncS4_2.   @error = " & @error & @CRLF)
    FuncS5_1()
    ConsoleWrite("FuncS5_1.   @error = " & @error & @CRLF)
    FuncS5_2()
    ConsoleWrite("FuncS5_2.   @error = " & @error & @CRLF)
EndFunc   ;==>Main

Func FuncR()
    RegRead("XXX", "")
EndFunc   ;==>FuncR

Func FuncS()
    RegRead("XXX", "")
    SetError(@error)
EndFunc   ;==>FuncS

Func FuncS2()
    RegRead("XXX", "")
    SetError(@error)
EndFunc   ;==>Func2

Func FuncS3()
    RegRead("XXX", "")
    SetError(@error)
    Return
EndFunc   ;==>FuncS3

Func FuncS4_1()
    RegRead("XXX", "")
    ConsoleWrite(@CRLF)
    ConsoleWrite('FuncS4_1: inner check: ' & @error & @CRLF)
    ConsoleWrite('FuncS4_1: inner check: ' & @error & @CRLF)
    ConsoleWrite('FuncS4_1: inner check: ' & @error & @CRLF)
    Return
EndFunc   ;==>FuncS3

Func FuncS4_2()
    RegRead("XXX", "")
    SetError(@error)
    ConsoleWrite(@CRLF)
    ConsoleWrite('FuncS4_2: inner check: ' & @error & @CRLF)
    ConsoleWrite('FuncS4_2: inner check: ' & @error & @CRLF)
    ConsoleWrite('FuncS4_2: inner check: ' & @error & @CRLF)
    Return
EndFunc   ;==>FuncS3

Func FuncS5_1()
    RegRead("XXX", "")
    ConsoleWrite(@CRLF)
    Return SetError(@error)
EndFunc   ;==>FuncS3

Func FuncS5_2()
    RegRead("XXX", "")
    SetError(@error)
    ConsoleWrite(@CRLF)
    Return SetError(@error)
EndFunc   ;==>FuncS3

 

so FuncS3 return error

Func FuncS3()
    RegRead("XXX", "")
    SetError(@error)
    Return
EndFunc   ;==>FuncS3

and FuncS4_2 does not return error

Func FuncS4_2()
    RegRead("XXX", "")
    SetError(@error)
    ConsoleWrite(@CRLF)
    ConsoleWrite('FuncS4_2: inner check: ' & @error & @CRLF)
    ConsoleWrite('FuncS4_2: inner check: ' & @error & @CRLF)
    ConsoleWrite('FuncS4_2: inner check: ' & @error & @CRLF)
    Return
EndFunc   ;==>FuncS3

Please note that each ConsoleWrite retains errors:

  Quote

The @error and @extended are not set on return leaving them as they were before calling.

Expand  

 

EDIT:
So in some sense they are actually retained I mean, until you want to return from the function.

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

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
×
×
  • Create New...