(newbye) exceptions list for python3 classes

alex23 wuwei23 at gmail.com
Mon Jun 24 19:22:20 EDT 2013


On 25/06/2013 7:43 AM, chrem wrote:
> Le 24/06/13 23:35, chrem a écrit :
>> what is the best way to find out all exceptions for a class?
>> E.g. I want to find out all exceptions related to the zipfile (I'm
>> searching for the Bad password exception syntax).

The only way is to look at the source code for the class and look for 
raise statements. However, not this will not show exceptions that may be 
raised by other objects it wraps or utilises, like file IO exceptions.

> without exception, it shown:
> RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at
> 0x100756690>)
>
> then I tried:
>      except zipfile.BadPassword:
>                          print("Password does not match")
> but it returned:
> AttributeError: 'module' object has no attribute 'BadPassword'

The exception is prefixed to the message, try catching RuntimeError instead:

     except RuntimeError as e:
         if 'Bad password' in e.message:
             print('Password does not match')
         else:
             raise e

In this example, I've re-raised the exception if it isn't a bad password 
problem, as it's not handled by my code. This is considered good 
practice: deal with the exceptions you can, ignore or re-raise the ones 
you can't.



More information about the Python-list mailing list