Trapping error for Region.wait ?

Asked by bengalih

code:

    if NextEpisodeRegion.wait("logo.png",8):

If the logo is not found, the following exception is presented:

Exception in thread "Thread-4" Line 61, in file test.py

        at org.sikuli.script.Region.handleFindFailed(Region.java:420)
        at org.sikuli.script.Region.wait(Region.java:511)
        at org.python.proxies.sikuli.Region$Region$0.super__wait(Unknown Source)

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)

org.sikuli.script.FindFailed: FindFailed: can not find logo.png on the screen.
  Line 61, in file test.py

This kind of gets in the way of my error output...I am wondering if there is a more graceful way to deal with this exception? I tried

findLogo = NextEpisodeRegion.wait("logo.png",8):
if findLogo.....

but I seem to get the same. I need to allow up to 8 seconds from the time a hotkey is triggered to see if the logo appears and if not then exit the def gracefully. Would i need to do a find instead? How would I delay this...if that's the answer, does it affect me in any other way (synchronous/async, etc...).

thanks!

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

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

if NextEpisodeRegion.exists("logo.png",8):

exists() returns True or False by handling FindFailed exception internally.

Revision history for this message
j (j-the-k) said :
#2

alternatives to RaiMan's suggestion:

1.
setThrowException(False) # you won't have any find failed exceptions any more

if NextEpisodeRegion.wait("logo.png",8) != None:
    # code if image was found
else:
    # code if image was not found

2.
try:
    NextEpisodeRegion.wait("logo.png",8):
    # code if image was found
except:
    # code if image was not found

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

@ j-the-k

yes of course ;-)

But to avoid these risky (1.) and ugly (2.) constructs we have exists()

Revision history for this message
bengalih (bengalih) said :
#4

Thanks RaiMan, that solved my question.

Revision history for this message
bengalih (bengalih) said :
#5

if NextEpisodeRegion.exists("logo.png",8):

OK...duh...it was late and i was focusing on region.find for some reason and didn't see a timeout value there...I wanted to figure it out before I went to bed so figured I'd ask and have an answer in the morning.

Anyway, thanks...could you advise on a practical purpose of using .wait over .exists then? If putting a timeout on .exists has the same net result as a .wait but without the ugly error output, why use it?

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

--- practical purpose of using .wait over .exists

I use exists(), if I need the True/False return value (making decisions) and in any other cases where I want to wait, but am not interested in FindFailed exceptions (if the waiting time is exceeded, I handle the situation in my script somehow).

I use wait(image, time), to wait for an image that should come up within the given time. If it does not, it makes no sense, that my script continues or it is too complex to handle the situation scripted - so it should simply crash.

Can you help with this problem?

Provide an answer of your own, or ask bengalih for more information if necessary.

To post a message you must log in.