try..except with empty exceptions
Cameron Simpson
cs at zip.com.au
Sun Apr 12 06:00:08 EDT 2015
On 12Apr2015 17:00, Chris Angelico <rosuav at gmail.com> wrote:
>On Sun, Apr 12, 2015 at 4:33 PM, Cameron Simpson <cs at zip.com.au> wrote:
[...]
>That's what try/finally is for. You can do your cleanup without caring
>exactly what was raised.
Hmm, yes.
[...]
>> However, my Asynchron class really is a little special. I use Asynchron to
>> underpin a bunch of classes whose instances get fulfilled later, especially
>> callables that get queued and run at a suitable time (or which get run in
>> response to some external long awaited event). ...
>> Anyway, at some point in the above example "function" gets called (by the
>> infrastructure of "Later") with the supplied arguments as the Later
>> processes its queue.
>>
>> So in this situation: should I catch and defer KeyboardInterrupt or
>> SystemExit? Maybe not, but the clean/naive implementation says to catch
>> absolutely everything.
>
>That is a tough one. I would support either of BaseException and
>Exception for this case, and whichever you pick, there will be a time
>when you wish you'd picked the other. But at least now you get a
>chance to think about it. [...]
On reflection I'm probably going to go after a bunch of my in-other-thread
things that handle "unknown" callables and do something like:
self.result = None
self.exc_info = None
try:
r = func(...)
except Exception:
self.exc_info = sys.exc_info
except BaseException as e:
self.exc_info = sys.exc_info
exception("unexpected BaseException: %s", e)
else:
self.result = r
to catch and report the weirdos. And I might write a little decorator to report
and reraise NameError to avoid certain times of silent asynchronous failure I
have to debug.
Colour me convinced, and +0.5 for proposals to retire bare "except".
Behold:
[hg/css]fleet*> hg clone . ../css-remove-except:
updating to branch default
2871 files updated, 0 files merged, 0 files removed, 0 files unresolved
Unnamed branch to change my practices:-)
Cheers,
Cameron Simpson <cs at zip.com.au>
... It beeped and said "Countdown initiated." Is that bad?
More information about the Python-list
mailing list