Letter replacer - suggestions?

Chris Angelico rosuav at gmail.com
Mon Dec 7 15:24:32 EST 2020


On Tue, Dec 8, 2020 at 7:11 AM Jon Ribbens via Python-list
<python-list at python.org> wrote:
>
> On 2020-12-07, Chris Angelico <rosuav at gmail.com> wrote:
> > On Tue, Dec 8, 2020 at 6:41 AM Grant Edwards <grant.b.edwards at gmail.com> wrote:
> >> On 2020-12-07, MRAB <python at 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.

For "log and reraise" handlers, actually I usually *do* want to see
those. The goal of these is to report the exception in some different
way from the normal one, and maybe figuring out why some subsystem is
dying inexplicably; and a KeyboardInterrupt would definitely be of
note there.

ChrisA


More information about the Python-list mailing list