Adafruit_Learning_System_Guides icon indicating copy to clipboard operation
Adafruit_Learning_System_Guides copied to clipboard

Avoid bare try-except

Open dhalbert opened this issue 4 years ago • 0 comments

A number of examples here have code like:

try:
    # something that might fail
except:
    return # or pass or something like that

Sometimes there's good reason for a "bare except", but a lot of times it could hide an unexpected error. It's better to list the expected exceptions explicitly [how's that for alliteration?]. An example is: https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/4879082f54df6728f5e8787fc1417e407c1d466e/Hallowing_Lightsaber/lightsaber_standard.py#L52-L57.

I think this might have meant to catch missing audio files, but it started catching audioio.WaveFile() failure (because now it's audiocore.WaveFile().) and not complaining. This made the failure non-obvious to an experienced user in the forums.

dhalbert avatar Jan 11 '21 21:01 dhalbert