IDE: exit in try: block throws Exception

Asked by Chetan on 2021-02-23

workaround see comment #1
-------------------------------------------------
*****
updated my comments for workaround

Hi
i wish to gracefully end my application. however when it use exit or exit(2) i always get an SystemExit exception
is there a way i can avoid systemexit exception?
build 2.0.4
 below is sample code
try:
    print("help")
    exit()

except:
    print("in except:")

 output
help
in except:

if i add traceback then below is detailed output
help
in except:
sky>in error
Traceback (most recent call last):
  File "C:\Perf\Work\Groups\AntiPirates\PDFNext\Automations\FeatTests\Common\UIAutomation\acrobat.sikuli\test1.sikuli\test1.py", line 4, in <module>
    exit()
  File "C:\Users\cshamdas\AppData\Roaming\Sikulix\Lib\sikuli\Sikuli.py", line 569, in exit
    sys.exit(code)
SystemExit: 0

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:
RaiMan (raimund-hocke) said : #1

apparently it is not possible to exit() without exception from a try: block

this works:

shouldExit = False
try:
    print "try"
    shouldExit = True
    #exit(1) --- this is not possible
except:
    print "except"

if shouldExit:
    print "shouldExit"
    exit(1)

Chetan (cshamdas) said : #2

Thanks Raiman for your comments
unfortunately this would not work for us beacuse:
1. we are having method list Fail() where we wish to stop the execution if condition fails else continue

code is something link
Try
print("try")
!check(xys)="true"
fail() //exit

print("condition2) # execute this is condition not met

def fail()
exit()

2. Test case is written by tester so tester would may not be able to write code after except or forget it sometimes. howver user can call framework methods that we create like fail etc.also we wish that soehow we can handle this at backend so tester writes mimimum code

Best RaiMan (raimund-hocke) said : #3

I do not understand, why the concept cannot be done without using try/except.

Chetan (cshamdas) said : #4

Thanks Raiman, i got now what you mean to say.
appreciated

Chetan (cshamdas) said : #5

Thanks RaiMan, that solved my question.