How do you find what exceptions a class can throw?

Grant Edwards grant.b.edwards at gmail.com
Sun Dec 20 13:25:40 EST 2020


On 2020-12-20, 2QdxY4RzWzUUiLuE at potatochowder.com <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
> Chris Green <cl at isbd.net> wrote:
>
>>>   Ultimately, it is not possible to tell what exceptions 
>>>   a call might throw.

While it may not be "ultimately possible", in practice it usually
is. Most libarary documentation lists exceptions thrown by fuctions
and explains what causes them.

>>>   In such a case, it may help to explain
>>>   to the newsgroup why this information is needed.
>>
>> So that, as is always advised, I can catch the specific exception
>> being thrown!
>
> To the point: what are you going to do with it once you catch it?

Seriously? You've never written code that handled an exception?

> Are you prepared to handle every exception individually,

Of course he isn't.

> or are you fishing for exceptions that you think might be worth
> catching?

You handle whatever exception(s) you expect, and let the others pass
up to higher levels. There are lots of cases where there may be one or
two exceptions that you want to handle.

> Remember, you get reporting (a traceback) and program cleanup and
> exit for free.  What will catching an exception *add* to the user
> experience?

Yes, if you can deal with it in such a way that normal execution of
the program (from the user's POV) continues. Exceptions aren't always
bugs and errors. Sometimes it's just an uncommon use-case that is
foreseen and dealt with.






More information about the Python-list mailing list