How to catch a generic exception?

Gonçalo Rodrigues op73418 at mail.telepac.pt
Thu Mar 4 10:55:07 EST 2004


On Thu, 04 Mar 2004 16:23:52 +0100, Boogie El Aceitoso
<frr149 at telefonica.net> wrote:

>Hi,
>
>How can I catch a generic exception (without knowing a priori what exception
>it will be)?
>

First answer: don't try to do it, you'll get bitten by it.

>For example, something like this
>try:
>   generateSomeException()
>except e:
>  print e
>

Second answer: The Exception class is the base class of all exception
classes. So, the following should work:

try:
    <code goes here>
except Exception, e:
    print e


Notice that you'll catch *every* exception, even a KeyboardInterrupt
(or whatever it's called) - are you sure that's what you want?

With my best regards,
G. Rodrigues



More information about the Python-list mailing list