(newbye) exceptions list for python3 classes

C

chrem

Hi,

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).

thanks for your help or feedback,
Christophe
 
C

chrem

Le 24/06/13 23:35, chrem a écrit :
Hi,

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).

thanks for your help or feedback,
Christophe

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'
 
A

alex23

Le 24/06/13 23:35, chrem a écrit :

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.
 
D

Dave Angel

Le 24/06/13 23:35, chrem a écrit :

without exception, it shown:
RuntimeError: ('Bad password for file', <zipfile.ZipInfo object at
0x100756690>)

then I tried:
except zipfile.BadPassword:

You cannot just make up a name, and hope that it's a valid exception
type. There is no such exception as zipfile.BadPassword. There is an
exception, zipfile.BadZipFile,
as mentioned at
http://docs.python.org/3.3/library/zipfile.html#zipfile.BadZipFile

But of course that's not what you want. The exception type above is
RuntimeError, the part before the colon.
 
C

chrem

Le 24/06/13 23:43, chrem a écrit :
Le 24/06/13 23:35, chrem a écrit :

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'
Thanks alex23, Dave Angel and Ben Funney for your precious feedbacks !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top