Jump to content

Setting the @error returned by a UDF in advance


TheDcoder
 Share

Recommended Posts

2 minutes ago, iamtheky said:

Do you normally leave that part out when you post it?

Not sure what you mean :think:

4 minutes ago, iamtheky said:

And I call large amounts of BS on anyone who claims to routinely seterror oustide of UDFs

Don't think many people use SetError outside of UDF.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

It was just clarifying 15 & 16.

The only reason the Func _Example() in the OP sets an error, is because it calls two other functions that both reset error.  Post 15 and 16 do not distinguish AutoIt internal functions from your Func _Example() when saying that ALL functions reset error.  It must be explicitly done, not something that happens by being a Func.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

2 minutes ago, iamtheky said:

The only reason the Func _Example() in the OP sets an error,

Actually, no, it does not set @error.

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

i said reset, i thought this whole thing determined sleep reset your error macro?

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

12 hours ago, iamtheky said:

i said reset, i thought this whole thing determined sleep reset your error macro?

Yes, exactly.

12 hours ago, iamtheky said:

Post 15 and 16 do not distinguish AutoIt internal functions from your Func _Example() when saying that ALL functions reset error.  It must be explicitly done, not something that happens by being a Func.

I was getting confused by this part. As far as I know, when a function is called, the @error macro is reset to 0 automatically... not sure what needs to be "explicitly" done. Here is an example:

SetError(1)

Example() ; Will output 0 to the console because @error is reset to 0 when a function is called

Func Example()
    ConsoleWrite(@error & @CRLF)
EndFunc
5 hours ago, czardas said:

You could use a binary flag (32 bit): which can be queried to trace where multiple errors occured.

No idea how that would work, that area is beyond my expertise :blink:. A more complete example might help me understand

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

EasyAsPie()

Local $iError = @error
If BitAND($iError, 1) Then ConsoleWrite("1st error occured" & @LF)
If BitAND($iError, 2) Then ConsoleWrite("2nd error occured" & @LF)
If BitAND($iError, 4) Then ConsoleWrite("3rd error occured" & @LF)

Func EasyAsPie()
    Local $iErrFlag = 0, $iExtFlag = 0, $vRetVal = 0

    $iErrFlag = BitOR($iErrFlag, 1) ; 1st error

    $iErrFlag = BitOR($iErrFlag, 2) ; 2nd error

    $iErrFlag = BitOR($iErrFlag, 4) ; 3rd error

    Return SetError($iErrFlag, $iExtFlag, $vRetVal)
EndFunc

 

Edited by czardas
Link to comment
Share on other sites

8 hours ago, TheDcoder said:

I was getting confused by this part

I certainly was nested.  What i get for mucking with ternary and bitwise (of which Czardas' example is legit) at the same time.

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Hi, inspired by @czardas I wrote this (because of just incrementing error counter by one):

#include <String.au3>

EasyAsPie()
Local $iError = @error
ConsoleWrite(_Integer2Binary($iError) & @CRLF)
_ShowError($iError)

Func _ShowError($iError)
    $bError = _Integer2Binary($iError)
    $bError = StringReverse($bError)
    $aError = StringSplit($bError, "")
    Local $sError
    For $i = 1 To $aError[0]
        If $aError[$i] = 1 Then
            $sError &= $i & ", "
        EndIf
    Next
    $sError = StringTrimRight($sError, 2) ; delete last ", "
    ConsoleWrite("Error(s): " & $sError & @CRLF)
EndFunc

Func EasyAsPie()
    Local $iErrFlag = 0, $iExtFlag = 0, $vRetVal = 0
    $iErrFlag = _AddError() ; error 1
    $iErrFlag = _AddError($iErrFlag, 2) ; error 2
    $iErrFlag = _AddError($iErrFlag, 3) ; error 3
    $iErrFlag = _AddError($iErrFlag, 4) ; error 4
    $iErrFlag = _AddError($iErrFlag, 5) ; error 5
    $iErrFlag = _AddError($iErrFlag, 31) ; error 31
    Return SetError($iErrFlag, $iExtFlag, $vRetVal)
EndFunc

Func _AddError($iErrFlag = 0, $iCount = 1, $sSize = "D") ; "B" Byte = 8; "W" Word = 16; "D" Dword = 32
    Local $bBitError = BitRotate(1, $iCount - 1, $sSize) ; shifting setted Bit #1 via $iCount left - stays there if $iErrFlag is 1
    $iErrFlag = BitOR($iErrFlag, $bBitError) ; adds the new error
    Return $iErrFlag
EndFunc

Func _Integer2Binary($in, $iBit = 32) ; by UEZ - modified
    If $in = 0 Then Return 0
    Local $bin
    While $in > 0
        $bin &= Mod($in, 2)
        $in = Floor($in / 2)
    WEnd
    Local $iAddNull = $iBit - Mod(StringLen($bin), $iBit)
    if $iAddNull < $iBit Then
        For $i = 1 To $iAddNull
            $bin &= 0
        Next
    EndIf
    $bin = StringReverse($bin)
    Return $bin
EndFunc

Any comments or suggestion to make it even more easier?

Conrad

Edited by Simpel
code correction
SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Link to comment
Share on other sites

Another possibility to show error:

Func _ShowError($iError)
    Local $sError
    For $i = 1 To 32
        If BitAND($iError, 2 ^ ($i - 1)) Then
            $sError &= $i & ", "
        EndIf
    Next
    $sError = StringTrimRight($sError, 2) ; delete last ", "
    ConsoleWrite("Error(s): " & $sError & @CRLF)
EndFunc

If you compare both variants the second is about 3 times faster.

Conrad

SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win_10   Build = 19044   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

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