In systems programming it is common to invoke some library function which may fail, and if it does fail, to check errno
for the exact cause. This is true even in Python, and I think it's more cumbersome than it needs to be. Let's take for example some code which tries to remove a file, and continues silently if the file did not exist:
try:
sftp.unlink(path)
except IOError as ex:
if ex.errno != errno.ENOENT:
raise
I'd like to know if it has ever been allowed or proposed in Python to do something more like this:
try:
sftp.unlink(path)
except IOError as ex if ex.errno == errno.ENOENT:
pass
I think this has a few things to recommend it:
- More concise.
- Familiar: as we can already catch certain types of exceptions, we catch only certain actual instances. An uncaught exception is propagated in the usual way.
- No new keywords required, nor any major new syntactic constructs.
I'd be surprised if this hasn't been considered before, so I'd accept as an answer any links to proposals past or pending. I'd also accept an answer explaining why the above would introduce any sort of problem with the existing language (Python 3.x, as I think 2.x is mostly frozen).