Custom error handling in Sikuli

Asked by Test

Hey, I've been trying to find out how to do this for a while.

I have a program which is step-based.

I want to be able to execute some code if and when my Sikuli script stops running. And it needs to be for ANY reason (invalid image found/user stops the program (shift+alt+c)/user quits the program when being prompted) except for reasons like invalid Python code.

Basically what I need to do if and when the program quits due to either user input or an error is I need to open up a file and write a step number to it. It's just the error handling that I'm a bit stumped about.

Some exploration has led me to believe that I'd need to do something with a try... except... statement. But I don't know how exactly to go about it.

Any help is appreciated. Thanks in advance.

P.S. There is a workaround I can easily use, which is to write the number of the step to the file as the user begins the step, but I feel that it's inefficient and bad practice to do it every single step rather than when it really needs to be done. But if there is no better way to do it then I'll have to fallback on that.

Question information

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

using version 1.0.1 (which has user logging including timestamp) ??

generally:
- to catch <FindFailed> you have to use try/except. handling takes place in the except block.
- to "catch" user input cancellation you have to wrap the input in a function, where you can do what is needed
- abort (shift+alt+c) is not catchable currently (but you might define your own <hotkey> and do what is needed in the handler function)

Come back if you need more (after having read through the docs http://doc.sikuli.org ;-) the keywords to look for: <see above>

Revision history for this message
Test (c4456517) said :
#2

So there is no way to have a one-time detection of the program being terminated (regardless of the reason)?

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

Currently there is no "exit-handler", that one could use for the case, that the script terminates for whatever reason.

But might be a good idea. I take it as a request.

Revision history for this message
Test (c4456517) said :
#5

Ok, thanks for your help.

Revision history for this message
obiwan-92 (obiwan-92) said :
#6

Hello,

Python like Java works with exception.

try:
    # Your code
except :
    # Your action where there is some problem.

Regards

Revision history for this message
Test (c4456517) said :
#7

Thanks RaiMan, that solved my question.