try..except with empty exceptions

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Apr 11 13:00:59 CEST 2015


On Sat, 11 Apr 2015 07:27 pm, Cameron Simpson wrote:

> If the empty tuple were to mean "catch everything" then there would not be
> a way to express "catch nothing". Bad bad bad!

# Catch everything:
try:
    spam()
except:
    pass


# Catch nothing:
spam()


:-)


> Consider this a proof that Python's current meanings for bare except and
> "except ()" are sensible, using a proof by contradiction.


Given that Python 3 does not allow you to raise things which don't inherit
from BaseException, I wish that bare except clauses were dropped
altogether. In Python 3, the equivalent to "catch everything" is
spelled "except BaseException", which I think would be a lot less
attractive to beginners than bare "except:".

But I digress.

Yes, I agree that Python's behaviour here is better than the alternative.
Having "except ()" catch nothing is consistent with the behaviour with
other tuples, so I'm okay with that. But it still surprised me :-)



-- 
Steven




More information about the Python-list mailing list