Jump to content

<SOLVED>Help with SetError


Recommended Posts

Hi, Im kind of confused with seterror, If I put Sleep SetError is working fine, but without sleep set error doesn't work. please can someone give me a good and easy example. TIA

Parameters:

code = I don't really understand this, is it ok to put any numbers??? or some numbers represent something?

extended = same situation, is it ok to put any integer???

#include <Array.au3>
#include <File.au3>
$sFilePath = "C:\New Folder"
$FLTAR = _FileListToArrayRec($sFilePath = "", "*.pdf", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH)
If @error Then $FLTAR = SetError(1, 0, "0 files for transfer")
Sleep(100)
If $FLTAR = @error Then
    MsgBox(0, "$FLTARIf", $FLTAR)
Else
    MsgBox(0, "$FLTARElse", "SUCCESS")
EndIf

I used 1 and 0 for testing only.

Edited by 232showtime
Marked Solved

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Link to comment
Share on other sites

See the related discussion here.

Any function called (including Sleep) will immediately (that is, before executing the called function) reset @error and @extended to 0 automatically, so that the function can then generate meaningful error codes upon return.

Link to comment
Share on other sites

I believe SetError() and SetExtended() are limited to int-32. I don't understand what you say about Sleep(). Errors are automatically reset to 0 when you enter a function: Sleep() is a function. The best way to handle SetError() is to use it as a return command from a function.

Example()
MsgBox(0, "Error from Example()", @error)
MsgBox(0, "Error from MsgBox()", @error)

Func Example()
    Local $sSomething = "Failed"
    If $sSomething = "Failed" Then Return SetError(1) ; like this!
EndFunc

You can see that @Error can be passed to a function as a parameter (eg. MsgBox), but the value changes immediately once you enter that function.

Edited by czardas
Link to comment
Share on other sites

@czardas

copy, paste and run my script then you will get the msgbox "0 files for transfer", next remove the sleep command from the script and run it again you will get the msgbox "SUCCESS"

thanks for the example...

Edited by 232showtime

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Link to comment
Share on other sites

Yeah that makes sense. Using Sleep() cancels any previous error. :think:

Edit... Check this:

$FLTAR = SetError(1, 0, "0 files for transfer")
MsgBox(0, "", $FLTAR)

That string will never be equal to an integer. Which implies that the following comparison should always fail.

If $FLTAR = @error Then

Actually it doesn't always fail, because AutoIt converts your striing to zero - which I think is unfortunate.

MsgBox(0, "False", @error = "0 files for transfer")

This seems an illogical result. So you have to be aware of, and avoid, internal conversions of this kind affecting your code logic.

Edited by czardas
Link to comment
Share on other sites

Some modifications to your code.

#include <Array.au3>
#include <File.au3>
Local $sFilePath = "C:\New Folder"
Local $FLTAR = _FileListToArrayRec($sFilePath = "", "*.pdf", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH)

Local $iError = @error ; we might need to refer to this later

If $iError Then
    MsgBox(0, "$FLTARIf", "0 files for transfer")
Else
    MsgBox(0, "$FLTARElse", "SUCCESS")
EndIf

 

Link to comment
Share on other sites

but what if there is a command line between $FLTAR and $iError and I want to get the error from $FLTAR,

#include <Array.au3>
#include <File.au3>

Local $a = 0
Local $sFilePath = "C:\New Folder"
Local $FLTAR = _FileListToArrayRec($sFilePath, "*.pdf", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH)

If $a = 0 Then MsgBox(0, "", $a)

Local $iError = @error ; we might need to refer to this later

If $iError Then
    MsgBox(0, "$FLTARIf", "0 files for transfer")
Else
    MsgBox(0, "$FLTARElse", "SUCCESS")
EndIf

 

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Link to comment
Share on other sites

Do not call any function between these two lines:

Local $FLTAR = _FileListToArrayRec($sFilePath = "", "*.pdf", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH)

Local $iError = @error ; we might need to refer to this later

Also check my edits to #5 above.

Edited by czardas
Link to comment
Share on other sites

13 minutes ago, 232showtime said:

but what if there is a command line between $FLTAR and $iError and I want to get the error from $FLTAR,

Just store @error in some temporary variable $error imediately, then evaluate that later on.:)

Link to comment
Share on other sites

You can store as many error values as you like, for as long as you like, using different variable names or even an array of error values. You could even store multiple errors as single integer using bitwise functions. There are many possible solutions to your query.

Edited by czardas
Link to comment
Share on other sites

 

1 hour ago, RTFC said:

Just store @error in some temporary variable $error imediately, then evaluate that later on.:)

do you mind giving me an example of yours??? just want to have some comparison

Edited by 232showtime

ill get to that... i still need to learn and understand a lot of codes graduated.gif

Correct answer, learn to walk before you take on that marathon.

Link to comment
Share on other sites

czardas was very very clear in posts #6 and #8   :)

#include <Array.au3>
#include <File.au3>

Local $a = 0
Local $sFilePath = "C:\New Folder"
Local $FLTAR = _FileListToArrayRec($sFilePath, "*.pdf", $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_FULLPATH)

; store @error IMMEDIATELY
Local $iError = @error

; then do some other things
If $a = 0 Then MsgBox(0, "", $a)
Sleep(100)
; etc

; then check previous $iError
If $iError Then
    MsgBox(0, "$FLTARIf", "0 files for transfer")
Else
    MsgBox(0, "$FLTARElse", "SUCCESS")
EndIf

 

Link to comment
Share on other sites

2 hours ago, 232showtime said:

I just want example from rtfc,

I fully support czardas's example in the direct case. When you're calling your own functions that may return errors, it gets slightly more complicated. In this example from my matrix computing environment (Eigen4AutoIt), I use SetError directly within a function body as early-out when various things go wrong:

Func _Eigen_Release_ExclusiveAccess($matrix, $timeoutInMs=0)

    If Not @AutoItX64 Then Return SetError(1,0,False)
    Local $mat_ptr=_Eigen_GetPtr($matrix)
    If @error Then Return SetError(2,0,False)

    Return _HighMem_ReleaseExclusiveAccess($mat_ptr, $timeoutInMs)  ; all other errors are dealt with by _HighMem
EndFunc

Here @error=1 in a 32-bit environment (not supported by HighMem), and 2 if no valid pointer is obtained. If all is well the Return argument being the real (NB _HighMem, not _Eigen, note the different prefix) function call, @error and @extended from that call are parsed upwards to whatever routine called _Eigen_Release_ExclusiveAccess (which returns True if successful). This can also be chained, like so:

Func _Eigen_Release_Mutex($matrix, $timeoutInMs=0)
    Local $tmp=_Eigen_Release_ExclusiveAccess($matrix, $timeoutInMs)
    Return SetError(@error, @extended, $tmp)
EndFunc

This second example is a so-called alias wrapper (exclusive access is controlled by a mutex, and since I always forget my own function names, I add tons of alias wrappers as mental crutches). Note how we collect whatever the true function returns in local variabe $tmp, and immediately afterwards, we parse the current contents of @error and @extended up the calling chain, preserving their status. Circling back to the original argument, if we wanted to do other stuff inbetween we'd have to collect those macro contents first:

Func _Eigen_Release_Mutex($matrix, $timeoutInMs=0)
    Local $tmp=_Eigen_Release_ExclusiveAccess($matrix, $timeoutInMs)
    Local $error=@error
    Local $extended=@extended
    < do other stuff...>
    Return SetError($error, $extended, $tmp)
EndFunc

That's how I do it.:)

Edited by RTFC
typos
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...