Prevent abrupt program shutdown

Asked by Connor Colenso on 2018-03-06

Sorry if this is super basic, I feel like it is but cant seem to formulate into a coherent sentence what I need to google.

Effectively im having an issue where I need to be able to press a specific button, this button then shortly after becomes replaced with another at a random interval. This is all well I can handle this, the issue is occasionally my loop tries to refind the original button in the short time before it can transition. Sorry if that doesn't make much sense, but in effect what I need is a way to make the program simply ignore a say hover command if the specific image cannot be found instead of causing the program to end. Many thanks and apologies if this is a wasting your time.

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Roman Podolyan
Last query:
Last reply:

If the problem is really that simple, you need just to put Hover into try-except block, and catch FindFailed exception

In my scripts I do this with functions like this:
def try_hover(our_image): # hover-function with exception catched
        except FindFailed:

If this is a single occurrence, feel free to use try-except without defining function.

More information on handling exceptions in Python / Jython can be found here:

Connor Colenso (eldertree) said : #3

Thanks Roman Podolyan, that solved my question.

RaiMan (raimund-hocke) said : #4

Another option always is, to use exists():

if exists(image, 0): # just one search
    # do what is needed
    # do something else