How do I know all thrown exceptions of a function?

Steve Williams sandj.williams at gte.net
Mon Jan 22 20:12:21 EST 2001


Carel Fellinger wrote:

> Steve Williams <sandj.williams at gte.net> wrote:
> ...
> > Catching exceptions thrown by imported classes is not easy, simple or clear,
> > IMHO.  The  solution requires a technique (adding the exception name to the
> > import statement) that is not common in Python documentation or examples.
>
> I'm not sure  I fully understand you, but something like the following
> seems common in Python:
>
> Module somelib:
>
>    class Error(Exception):
>         pass
>
>    def somefunc():
>        '''does something
>           beware, could raise Error
>        '''
>        raise Error
>
> Module MyProg:
>
>    import somelib
>
>    try:
>         somelib.somefunc()
>    except somelib.Error:
>         'do something about it'
> --
> groetjes, carel

Ah yes, but ftplib (and the example in the code and in Beazley) does the
following:

Module somelib:

   myError = "baderror"

   class Error(Exception):
        pass

   def somefunc():
       '''does something
          beware, could raise myError
       '''
       raise myError

Module MyProg:

   from somelib import somefunc

   try:
        somelib.somefunc()
   except somelib.myError:
        'do something about it'

Not at all the same thing.






More information about the Python-list mailing list