On 2020-12-07, Chris Angelico <ros...@gmail.com> wrote: > On Tue, Dec 8, 2020 at 6:41 AM Grant Edwards <grant.b.edwa...@gmail.com> > wrote: >> On 2020-12-07, MRAB <pyt...@mrabarnett.plus.com> wrote: >> > Avoid a 'bare' except unless you _really_ mean it, which is >> > virtually never. Catch only those exceptions that you're going to >> > handle. >> >> And sometimes "handling" is just printing some extra stuff and then >> re-raising the original exception: >> >> try: >> something(): >> except: >> print(<whatever might be helpful for troubleshooting>) >> raise >> > > Even there, I'd most often use "except BaseException as e:", other > than in a very few situations. The only time I have recently used a > bare except is when making use of the traceback module: > > try: > ... > except: > with open("notes.err", "a") as err: > traceback.print_exc(file=err) > raise > > since print_exc() can go fetch the exception via sys.exc_info().
... but even if you do think you want "except BaseException:" or "except:", you almost never actually do - you almost certainly want "except Exception:", because the former two will stop sys.exit() from working, or the user from pressing ctrl-C. -- https://mail.python.org/mailman/listinfo/python-list