← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #162176]: [HowTo] User defined exceptions to handle FindFailed

 

Question #162176 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/162176

RaiMan posted a new comment:
--- ok, here you are ;-) 
*** User defined exceptions in Sikuli scripts (Python/Jython)

the basic template is:

class MyException(Exception):
    def __init__(self):
        pass
    def __str__(self):
        # this string complements an error message
        return "This is my own Exception"

usage in your code:

raise MyException()

You will get the standard traceback with the last line as
__main__.MyException: This is my own Exception

or handled:

try:
   if some_condition: raise MyException()
except MyException:
   print "we had MyException"
   exit()

you might have to additionally handle the traceback information and/or
sys.exc as normally with try: except:

So the above situation could be handled with your own exception as well 
(but I think in a Sikuli script any FindFailed exception handling looks much more ugly then using exists()):

def handler():
    print "I am the handler for FindFailed"
    return "some information"

class MyException(Exception):
    def __init__(self, handler):
        self.status = handler()
    def __str__(self):
        # this string complements an error message
        return "We handled a FindFailed exception"

if not exists("some-image.png"): raise MyException(handler)

and with try: except:

try:
   if not exists("some-image.png"): raise MyException(handler)
except MyException, e:
   print "we made it:", e.status

-- 
You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.