find() if not found throws exception - has problems

Bug #523660 reported by RaiMan
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SikuliX
Fix Released
Undecided
Unassigned

Bug Description

documentation says:

setThrowException(flag) [#]
Sets the flag of throwing exceptions if find() fails.
Setting this flag to True enables all methods that use find() throws an exception if the find() can not find anything similar on the screen. Once the flag is set to False, all methods that use find() just return None if nothing is found.
The default value of thie flag is True.

1. default value is False. you have to say setThrowException(True) to catch not-found with try: except:
2. Class defs give the impression, that an error FindFailed is defined, but except FindFailed: gives name error
3. saying except Exception: gives a java crash in the find()
4. finally: gives a syntax error
5. sys.exc_info() works, module traceback does not work as expected

this is what works:

import sys, re
setThrowException(True)
myErr = False
expr=re.compile(r"edu.mit.csail.uid.FindFailed:\s(.*\s(\d*.png))")

try:
   click( )

except:
   err = str(sys.exc_info()[1])
   m = expr.match(err)
   myErr = True
   if m:
      print "++++ FindFailed ++++ "+m.group(2)
      popup(m.group(1))

Revision history for this message
RaiMan (raimund-hocke) wrote :

Want to know my final well running version for now?

look: How to do an easy not-found handling? (https://answers.launchpad.net/sikuli/+question/101431)

Changed in sikuli:
status: New → Fix Committed
Revision history for this message
RaiMan (raimund-hocke) wrote :

CORRECTION:

above i stated:
4. finally: gives a syntax error

This is not true, since I used finally: in a wrong fashion, since I'm new to Python.

The truth is:
try... finally and try... except... else can not be used together in one try:, but can be nested.

So if you want to have something done in all cases AND catch exceptions, you have to write:

try:
  pass # code 1 - optional
  try:
     pass # code 2 - executed if code 1 does not raise an exception
  finally:
     pass # code 3 - executed always if code 2 is executed even if it raises an exception
  pass # code 4 - executed if code 1 AND code 2 do not raise an exception
except:
  pass # code 5 - executed if code 1 OR code 2 raise an exception
else:
  pass # executed for exceptions not caught by any except before

Revision history for this message
RaiMan (raimund-hocke) wrote :

With version 0.9.9 problem solved:
- default as documented setThrowExeption(True) (want find()= returns None: setThrowExeption(False))
- except FindFailed: works
- sys.exc_info() contains valuable information

Changed in sikuli:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Related questions

Remote bug watches

Bug watches keep track of this bug in other bug trackers.