How to implement Try Catch error handling in Sikuli Script

Asked by Philippe Trounev

Hello,
 When I try to implement the try catch functionality in my script.
Let me explain the scenario first.

I have a file with data
The script goes through each row of the data and dynamically populates data in the software from row 1 and than once it completes the operation it moves to row 2 and continues with the next row of data.

I implemented try catch functionality that should work as follows
try:
   somefunction(passed)
except Exception:
  return(failed)

than the script does a clean up

Basically when I try to implement try, catch functionality and an unexpected issues comes up, I still end up getting internal sikuli errors of image not found, so instead of function returning a failure and continuing on with doing a clean up and moving on to the next row, it fails out in the ide with image not found error.

[error] Stopped
[error] An error occurs at line 117
[error] Error message: Traceback (most recent call last):
 File "C:\DOCUME~1\xxxx\LOCALS~1\Temp\sikuli-tmp8064744401480455439.py", line 117, in
 status = CC_FindFA()
 File "C:\DOCUME~1\xxxx\LOCALS~1\Temp\sikuli-tmp8064744401480455439.py", line 69, in CC_FindFA
 wait(Pattern("gSaveInterac-1.png").similar(0.32))
 File "C:\Documents and Settings\xxxx\My Documents\Sikuli X\sikuli-script.jar\Lib\sikuli\Region.py", line 99, in wait
 Line 87, in file C:\DOCUME~1\xxxxx\LOCALS~1\Temp\sikuli-tmp8064744401480455439.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.GeneratedMethodAccessor53.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 Pattern("gSaveInterac-1.png").similar(0.32) on the screen.
 Line 87, in file C:\DOCUME~1\xxxx\LOCALS~1\Temp\sikuli-tmp8064744401480455439.py

So my question is, is it possible to do a try an catch error handling with sikuli, and if so how can be implemented?

Thank you.
P.T.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Philippe Trounev
Solved:
Last query:
Last reply:
Revision history for this message
rob (reg82) said :
#1

if you want to catch FindFailed Exceptions you have to do something like

try:
    some code...
except FindFailed:
    some code...

on the other hand if your trying to catch a a different Exception but FindFailed errors are prematurely ending your script, go back and figure out why sikuli cant find the images and fix the problem.

Revision history for this message
Philippe Trounev (ptrounev) said :
#2

Thank so much Robert, I tested it and it works.
 I am only using this form of exception handling as the last result, while running very long jobs. So instead of failing it simply outputs failed + jobname into the file.

Thank you again.

P.T.

Revision history for this message
stewazy (stewazy) said :
#3

How to I catch the error message to a log file ?