exit() inside try/except

Asked by fernando gandini

Hi folks,

im having a insue with exit() inside of try/except...
when the "try" found some image, then a popup appear and when user click "Ok", sikuli should exit...but it dont, sikuli keep runing..
any one know what im doing wrong?
thanks

here is my code:
try:
   regVig.find(Pattern("Ocarreramexc.png").similar(0.50).targetOffset(-2,43))
   pressKEY(1,Key.ENTER)
   popup("Ocorreram exceções neste cálculo!".decode('utf-8'))
   exit()
except:
   wait(8)
   calcular()

Question information

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

Here is working code:

====
import sys

try:
 hover("1342096962957.png")
 popup('Ok!')
 sys.exit()
except FindFailed:
 pass

popup('There was no exit!')
====

You should import sys module and call exit function right.

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

@Roman ;-)
import sys
not needed in Sikuli scripts, since sys and time are already imported

The problem with Sikuli's exit() in a try, is that it itself throws some exception deep inside (which is still a bug of course;-)

The advantage of using exit() is, that it internally processes some (minor) script finalizing, which helps to avoid some oddities that might happen in the IDE.

So if you want to use exit():

shouldExit = false
try:
   regVig.find(Pattern("Ocarreramexc.png").similar(0.50).targetOffset(-2,43))
   pressKEY(1,Key.ENTER)
   popup("Ocorreram exceções neste cálculo!".decode('utf-8'))
   shouldExit = true
except:
   wait(8)
   calcular()
if shouldExit: exit()

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

BTW this could all be much simpler:

if regVig.exists(Pattern("Ocarreramexc.png").similar(0.50).targetOffset(-2,43)):
   pressKEY(1,Key.ENTER)
   popup("Ocorreram exceções neste cálculo!".decode('utf-8'))
   exit()
else:
   wait(8)
   calcular()

Revision history for this message
fernando gandini (fernando-gandini) said :
#4

hi...

Roman thanks for the answer but its not work...

RaiMan, both answers works fine..

Thanks

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

> Roman thanks for the answer but its not work...

Strange, recipe taken from my working code and tested myself before posting. May be some misunderstanding.

RaiMan anwers worked, so it seems that all end good anyway.