Python: try ... except for wildcards for exception names?

I want only the exceptions thrown by the mutagen. However, there are many exceptions. Is there a way that I can wildcard (via regexp / etc) exceptions handled with exception? The alternative is just ugly ...

mutagen.apev2.APEBadItemError
mutagen.apev2.APENoHeaderError
mutagen.apev2.KeyError
mutagen.apev2.ValueError
mutagen.easyid3.EasyID3KeyError
mutagen.easyid3.KeyError
mutagen.easyid3.ValueError
mutagen.flac.FLACNoHeaderError
mutagen.flac.FLACVorbisError
mutagen.flac.TypeError
mutagen.id3.EnvironmentError
mutagen.id3.EOFError
mutagen.id3.ID3BadCompressedData
mutagen.id3.ID3BadUnsynchData

etc .: P

+3
source share
2 answers

There’s a less ugly way to go, although it’s still a little pain, each of these modules implements a “bug” due to which all the corresponding bugs propagate.

# Please note, the exception class truly is lower cased as indicated
mutagen.id3.error
mutagen.flac.error
mutagen.apev2.error

# mutagen.easyid3 errors extend the mutagen.id3.error class
+4
source

, - , , . , .

>>> errors = {NameError:'a', ValueError:'b'}
>>> try:
...     cornucopia
... except Exception as e:
...     e_type = type(e)
...     if e_type in errors:
...         print errors[e_type]
...     else:
...         raise
... 
a

, , ; , , . , , .

+2

All Articles