On Wed, Feb 20, 2019 at 3:26 AM Grant Edwards <grant.b.edwa...@gmail.com> wrote: > > On 2019-02-19, Chris Angelico <ros...@gmail.com> wrote: > > > Oh, and not just bugs either. If the user hits Ctrl-C at just the > > right moment, KeyboardInterrupt will be raised. You'll swallow that > > exception silently, preventing the user-requested halt, and going and > > doing the wrong thing. Don't use a bare except clause even if your > > code is 100% perfect every time. (And anyone who thinks their code is > > perfect hasn't tested it.) > > IMO, you're allowed to use a bare except clause to do cleanup or > logging as long as the execption handler ends with the line > > raise >
Yeah, I was clearer about that in the first email, to which that was a followup, but I'll give two examples of valid bare-except constructs: 1) Handle-and-raise try: weird stuff except: show failure state raise 2) Log-and-continue while True: try: thing = get_next_thing() except NoMoreThings, KeyboardInterrupt: break try: response = some_handler(thing) except: response = 500 log_error() try: thing.respond(response) except OSError: pass The implication of the second one is that (a) bugs in some_handler() shouldn't break the whole program, and (b) attempting to halt some_handler() with Ctrl-C shouldn't halt the whole program. It creates a boundary. But you should ALWAYS either reraise or log the exception. ALWAYS. Otherwise, be specific about what you're swallowing. ChrisA -- https://mail.python.org/mailman/listinfo/python-list