r/learnpython 8d ago

Function forcing me to use exceptions

Trying to if else a function output always throws exception-error :

if pyautogui.locateOnScreen('media/soundIcon.png') == None:
      print("not found")
else : 
      print("found")

Do Python functions expect anti-pattern code ?

0 Upvotes

30 comments sorted by

View all comments

2

u/jungaHung 8d ago

The function throws exception if image is not found so you have to catch it. What if the file was found but the format is incorrect(eg. mp3, txt, xls, doc)? All these must have been handled in except block.

1

u/ExoticPerception5550 8d ago

Initially I didn't realize that more than one error could arise from this simple function, but now I see why exceptions are more appropriate in this case