
Cameron Simpson wrote:
try: foo(bah[5]) except IndexError as e: ... infer that there is no bah[5] ...
One can easily want, instead, some kind of "shallow except", which would catch exceptions only if they were directly raised from the surface code;
The problem I see with that is how to define what counts as "surface code". If the __getitem__ method of bah is written in Python, I don't see how you could tell that an IndexError raised by it should be caught, but one raised by foo() shouldn't. In any case, this doesn't address the issue raised by the OP, which in this example is that if the implementation of bah.__getitem__ calls something else that raises an IndexError, there's no easy way to distinguish that from one raised by bah.__getitem__ itself. I don't see any way to solve that by messing around with different try-except constructs. It can only be addressed from within bah.__getitem__ itself, by having it catch any incidental IndexErrors and turn them into a different exception. -- Greg