On Fri, Jun 20, 2014 at 8:28 AM, Grant Edwards <invalid@invalid.invalid> wrote:
> On 2014-06-20, Mark Lawrence <breamore...@yahoo.co.uk> wrote:
>> For the OP a very important rule of thumb is never use a bare except, so
>> this is right out.
>> try:
>>      doSomething()
>> except:
>>      WTF()
> IMO, that sort of depends on WTF() does. One case where a bare except
> is well used is when stdandard output/error are not going anywhere
> useful and you want to log the exception and then terminate:
> try:
>     whatever()
> except Exception as e:
>     syslog("foobar: terminating due to unhandled exception %s.\n" % e)
>     sys.exit(1)

Logging unhandled exceptions and exiting is the job of sys.excepthook,
so I would prefer to replace it with a custom exception handler in
this case.

Also, this isn't an example of a bare except, which is an except
clause with no exception class specified. "except:" and "except
Exception:" are not equivalent. In Python 3, I believe that "except:"
and "except BaseException:" are equivalent. In Python 2 they are not,
because exceptions are also allowed to be old-style classes. In any
case, the advice against bare excepts stems from the fact that bare
excepts will catch things that you usually should not try to catch,
such as SystemExit and KeyboardInterrupt, and so you should normally
specify "except Exception:" in the most general case.

Reply via email to