using try and except

Asked by Maxo K.

Hello,

I am currently using a small script using: try and except

Code:

try:

Some code goes in here

except:

send email
-----------------------

It works pretty fine I would say, but I want to add an additional code which would assume the following: I want to put an additional layer between try and except if possible. So the script will run "try" function and in case it fails it runs the second function: try2 and if try2 is successful, then it should run "try" function once again. If "try2" fails, then it should do "except".

Did I explain well?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Maxo K.
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

Looping: FAQ 1437

success = False
while not success:
   try:
      # code layer 1
      success = True # this ends the loop
   except:
      try:
         #code layer 2
         continue # goes back to beginning of loop
      except:
         pass
   except:
      pass
   if not success:
      # here goes what you had before in your one except
      break # you might wish to exit the loop here
# first statement after the loop
if not success:
   popup("permanent error")
   exit(1)
# we go on if successful

I guess you are asking this, because you want to catch FindFailed exceptions. You should have a look at exists() which returns None in case of FindFailed, so it can be used in ii/elif/while constructs:

while not exists(<some-image>):
    # do something to make <some-image> come into existence
click(<some-image>) # in many cases you might use
                                # click(getLastMatch()) and avoid the same find() op

This usually makes the code more pretty against these ugly (even nested) <try: except:>.

see: http://sikuli.org/docx/region.html#exception-findfailed

Revision history for this message
Maxo K. (malkhaz) said :
#2

Thanks a lot once again RaiMan. Will try the above