Jump to content

How to "continueloop" whenever ANY line of the loop code returns @error <> 0?


Recommended Posts

I've a loop containing many lines of code.

I'ld like to restart ("continueloop") whenever any line of this code returns @error <> 0.

Is there a shorter/cleaner way to do it than to insert a "if @error <> 0 then continueloop" between each line of it?  (hundreds of lines!)

Link to comment
Share on other sites

Use an array of instructions inside a nested loop. Loop through all instructions in about 4 or 5 lines of code (use helper functions wherever you can). Add the error check (one time only) after the current instruction in the loop. If you want to go back to an earlier instruction (after an error occurs) then you can reset the loop iteration count.

Without seeing your code it's impossible to know if this will work. It nearly always does work. Try to simplify the task using less commands while you test the method.

Edited by czardas
Link to comment
Share on other sites

do you mean an array like this?

local $array[3] = ["instruction one...", "instruction two...", "instruction three..."]

to be executed in a loop by a "Execute ( string )" AutoIT function followed by the error check?

 

Yes, but it's not the only method (don't use Execute unless you need to). It was a concept / idea. Implementation will depend on circumstances. This is not the best code I have ever written, but it illustrates the idea. You will have to see how much you can simplify your own code. Using @error with Execute() can get complicated and tricky though.

;

Global $g_iInteger = 2

Local $avInstructions[5]
$avInstructions[0] = "_PowerOfTwo(1)"
$avInstructions[1] = "_PowerOfTwo(2)"
$avInstructions[2] = "Sleep(1000)" ; Native function
$avInstructions[3] = "_PowerOfTwo(3)"
$avInstructions[4] = "_PowerOfTwo(4)"


For $i = 0 To UBound($avInstructions) -1
    If Execute($avInstructions[$i]) <> 1 Then ; Failure on function call
        $g_iInteger += 1 ; Careful to avoid an infinite loop
        $i = -1 ; Go back to the start - $i will be incremented to zero on the next iteration
    EndIf
Next

Func _PowerOfTwo($iPower) ; Helper function
    Local $iValue = $g_iInteger ^ $iPower
    If $iValue = 8 Then Return 0 ; Forced Failure
    ConsoleWrite($iValue & @LF)
    Return 1 ; Success
EndFunc

;

You might want to think about setting a global flag to indicate when a function returns an error. As I stated earlier: there are various possible approaches to something like this.

p.s. That helper function should be called _PowerOfInt() :shhh:

Edited by czardas
Link to comment
Share on other sites

Nice idea... but I think that it's not sufficiently flexible...

 
What I have is a long IE automation script where now I want that, whenever a line of code of a sequence of lines fails (for example an _IENavigate action because the remote page loading reach timeout), AutoIT goes back to the starting line of the sequence (for example to the line that does the "login" action) and retry it (just) a second time.

I think that the lines are too much heterogeneous (there is not only "_IENavigate") to be reduced to a single "_PowerOfTwo"-like function or to just a few functions...
Link to comment
Share on other sites

  • Moderators

Again, without be able to see your code, you're first asking us to guess at precisely what you're doing, and then help you troubleshoot. Post your code, first off. Secondly, would your time and effort not be better spent removing errors from your code, rather than trying to get it to restart when it crashes?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

The reason why I don't post a specific code is because I'm thinking more to a solution to use for my IE automation script to come, in future months.
The scripts will basically cyclically navigate into websites and do many XML validation checks and data extractions.
Since these websites are outside my control, if any action over it (page load, XML validation check or data extraction) fails, most of times I can't do nothing to correct the error. When the error occurs I would like to log it on file and maybe *immediately* retry the sequence a second time without waiting the next cycle...

Edited by Imbuter2000
Link to comment
Share on other sites

  • Moderators

So, you want to create a script that does "many xml validation checks and data extractions", don't want to share any code on how you plan to do this, but want every conceivable error captured and some AI component to retry the function which the error is in, knowing exactly where it left off. Well, hell, I thought you were asking for something difficult... <_<

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Whipping out my crystal ball to see if I can provide some help....damn thing has a crack in! What the hell??? I got told by that nice gentleman in the trashy clothes at the flew market this thing really works! He wouldn't let me take it out of the box to check it. ....grumble....

..... back to reality....

Ether post your code for what you have in mind or not, it is up to you.

Expecting us to read your mind and see your code you have on your PC with a broken crystal ball....keep dreaming.

Link to comment
Share on other sites

can use one line by using the return values instead of errorcode like so. offcourse you have to adjust it to whatever return value that paticulair funtion uses incase of error

If _myfunction() = 0 then ContinueLoop
Edited by Djarlo
Link to comment
Share on other sites

Ok, here is a minimalist example:
 

while 1
    for $counter = 1 to 2  ; sequence for Gmail
        $handle = WinHttp_login("www.gmail.com","myusername_gmail","mypassword_gmail")
        if @error <> 0 then ContinueLoop
        $page = WinHttp_load_page($handle)
        if @error <> 0 then ContinueLoop
        XML_validation_gmail_1($page)
        if @error <> 0 then ContinueLoop
        XML_validation_gmail_2($page)
        if @error <> 0 then ContinueLoop
        $result = Data_extraction_gmail($page)
        if @error <> 0 then ContinueLoop
        exitloop
    next

    for $counter = 1 to 2  ; sequence for Hotmail
        $handle = WinHttp_login("www.hotmail.com","myusername_hotmail","mypassword_hotmail")
        if @error <> 0 then ContinueLoop
        $page = WinHttp_load_page($handle)
        if @error <> 0 then ContinueLoop
        XML_validation_hotmail_1($page)
        if @error <> 0 then ContinueLoop
        $result = Data_extraction_hotmail($page)
        if @error <> 0 then ContinueLoop
        exitloop
    next

    sleep(1000*60*30) ; 30 minutes
WEnd

Searching for a solution to obtain the same (double retry of the sequence) if something fails in a sequence (for example because of connection problems... or temporary alternative/maintainance text in the website) avoid repeating that "if @error <> 0 then ContinueLoop"...    (hundreds of time in real case).

Alternatively, I would to break the sequence (jump the following steps) if something fails in it, for example by changing the "for $counter = 1 to 2" to "for $counter = 1 to 1", always avoiding to repeat the "if @error <> 0 then ContinueLoop".

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