On 18/04/19 8:29 AM, Chris Angelico wrote:
On Thu, Apr 18, 2019 at 6:21 AM DL Neil <pythonl...@danceswithmice.info> wrote:
Do you bother with exception handling for import statements?
Can we assume that if such a catastrophic error occurs, it is quite
acceptable for the code to fall-over in a tumbling-fumble?

I try/except around import statements only if it's possible for the
program to recover from the exception. For instance, something that
...

User reactions have been covered elsewhere 'here'.


For something that is absolutely required for the program to continue,
what would be in your exception handler? Print a message to stderr and
exit? That's exactly what not-catching-the-exception is for. I do
sometimes annotate the imports, though:

We disagree. Although I hasten to add, if the error is trapped, then there should be some added-value over what is system-provided.

Application's log? SysLog? Particularly in a centralised or server-based application especially web-services.


from dataclasses import dataclass # ImportError? Upgrade to Python 3.7
or pip install dataclasses

If that bombs out, the entire line will get printed, comment and all,
and there isn't really anything else that I would want to do with the
exception.
So I guess the best way to answer your question is with another
question: If such a catastrophic error occurs, what ELSE would your
code do than fall over? If there's an answer to that question, then
sure, catch the ImportError. Otherwise don't.

Kudos! To whom are you addressing such annotations?

--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to