try..except with empty exceptions

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Apr 11 03:42:37 CEST 2015


On Sat, 11 Apr 2015 05:31 am, sohcahtoa82 at gmail.com wrote:

> It isn't document because it is expected.  Why would the exception get
> caught if you're not writing code to catch it?  If you write a function
> and pass it a tuple of exceptions to catch, I'm not sure why you would
> expect it to catch an exception not in the tuple.  Just because the tuple
> is empty doesn't mean that it should catch *everything* instead.  That
> would be counter-intuitive.

Really? I have to say, I expected it.


try:
    spam()
except This, That:
    # Implicitly a tuple of two exceptions.
    pass


Compare:

try:
    spam()
except:
    # Implicitly an empty tuple.
    pass


I'm not surprised that it fails, especially in Python 2 before
the "except ... as err" syntax was available, but the OP is not alone in
thinking that an empty tuple should catch everything.


I'm fairly dubious about catching everything, that sounds like a good way to
hide bugs, but if you need to catch everything, using Exception is the
usual way to do it.




-- 
Steven




More information about the Python-list mailing list