Prevent abrupt program shutdown

Asked by Connor Colenso

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

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Roman Podolyan
Solved:
Last query:
Last reply:
Revision history for this message
Best Roman Podolyan (podolyan-roman) said :
#1

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
        try:
                hover(our_image)
        except FindFailed:
                pass
_______

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

Revision history for this message
Roman Podolyan (podolyan-roman) said :
#2

More information on handling exceptions in Python / Jython can be found here:
https://docs.python.org/2/tutorial/errors.html#handling-exceptions

Revision history for this message
Connor Colenso (eldertree) said :
#3

Thanks Roman Podolyan, that solved my question.

Revision history for this message
RaiMan (raimund-hocke) said :
#4

Another option always is, to use exists():

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