Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49088 > unrolled thread

(newbye) exceptions list for python3 classes

Started bychrem <usenetNoSpam@chr1st0ph9.eu>
First post2013-06-24 23:35 +0200
Last post2013-06-25 09:27 +1000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  (newbye) exceptions list for python3 classes chrem <usenetNoSpam@chr1st0ph9.eu> - 2013-06-24 23:35 +0200
    Re: (newbye) exceptions list for python3 classes chrem <usenetNoSpam@chr1st0ph9.eu> - 2013-06-24 23:43 +0200
      Re: (newbye) exceptions list for python3 classes alex23 <wuwei23@gmail.com> - 2013-06-25 09:22 +1000
      Re: (newbye) exceptions list for python3 classes Dave Angel <davea@davea.name> - 2013-06-24 22:09 -0400
      Re: (newbye) exceptions list for python3 classes chrem <usenetNoSpam@chr1st0ph9.eu> - 2013-06-25 17:55 +0200
    Re: (newbye) exceptions list for python3 classes Ben Finney <ben+python@benfinney.id.au> - 2013-06-25 09:27 +1000

#49088 — (newbye) exceptions list for python3 classes

Fromchrem <usenetNoSpam@chr1st0ph9.eu>
Date2013-06-24 23:35 +0200
Subject(newbye) exceptions list for python3 classes
Message-ID<51c8bb87$0$2407$426a74cc@news.free.fr>
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

[toc] | [next] | [standalone]


#49091

Fromchrem <usenetNoSpam@chr1st0ph9.eu>
Date2013-06-24 23:43 +0200
Message-ID<51c8bd9d$0$18756$426a34cc@news.free.fr>
In reply to#49088
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'



[toc] | [prev] | [next] | [standalone]


#49107

Fromalex23 <wuwei23@gmail.com>
Date2013-06-25 09:22 +1000
Message-ID<kqak1g$70p$1@dont-email.me>
In reply to#49091
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.

[toc] | [prev] | [next] | [standalone]


#49118

FromDave Angel <davea@davea.name>
Date2013-06-24 22:09 -0400
Message-ID<mailman.3792.1372126168.3114.python-list@python.org>
In reply to#49091
On 06/24/2013 05:43 PM, chrem wrote:
> 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:

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.


>                          print("Password does not match")
> but it returned:
> AttributeError: 'module' object has no attribute 'BadPassword'
>
>
>
>


-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#49166

Fromchrem <usenetNoSpam@chr1st0ph9.eu>
Date2013-06-25 17:55 +0200
Message-ID<51c9bd67$0$2413$426a74cc@news.free.fr>
In reply to#49091
Le 24/06/13 23:43, chrem a écrit :
> 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'
>
>
>
>
Thanks alex23, Dave Angel and Ben Funney for your precious feedbacks !

[toc] | [prev] | [next] | [standalone]


#49108

FromBen Finney <ben+python@benfinney.id.au>
Date2013-06-25 09:27 +1000
Message-ID<mailman.3787.1372116473.3114.python-list@python.org>
In reply to#49088
chrem <usenetNoSpam@chr1st0ph9.eu> writes:

> Hi,

Howdy, congratulations on finding the Python programming language.

> what is the best way to find out all exceptions for a class?

Python is not Java. Your program doesn't need to know everything that
might happen; you should catch only those exceptions you can actually
deal with usefully, and let any others propagate.

> E.g. I want to find out all exceptions related to the zipfile (I'm
> searching for the Bad password exception syntax).

The documentation will describe the behaviour of the module; many
exceptions are implied by that (e.g. if the module deals with the
filesystem, you can expect OSError to be raised under certain
conditions).

Other exceptions will come from deeper within Python; e.g. if you give
text in a bad encoding you can expect UnicodeDecodeError, or if you give
it an object that can't be handled as expected you might get a TypeError.

But trying to get a complete set of all exceptions that might be raised
is a fool's errand. It would be futile, both because everything in
Python is an object and can therefore raise whatever exceptions are
appropriate, and also because even if you knew a complete set, you can't
usefully do anything consistent with such a huge set of exception types.

So, what is it you want to do one you know a set of exception types that
can be raised by the code?

> thanks for your help or feedback,

Hope that helps, and good fortune to you in learning to code Pythonically!

-- 
 \          “When we talk to God, we're praying. When God talks to us, |
  `\         we're schizophrenic.” —Jane Wagner, via Lily Tomlin, 1985 |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web