[Python-ideas] syntax to continue into the next subsequent except block

Joshua Landau joshua.landau.ws at gmail.com
Sun Sep 16 22:51:41 CEST 2012

Instead of adding syntax, we could make except use isinstance.

This means that those people who are using exceptions with
poor hierarchies can override the classes with their own. This makes for an
ugly setup but a very readable result. If you put the class definitions in
another file and import them, it may end up looking quite clean.

Please forgive me if I am doing this wrong, as I've never done this sort of
thing before.

class SpecificOSErrorType(type):
 def __instancecheck__(cls, othercls):
if isinstance(othercls, OSError):
 if othercls.errno == self.errno:
return True
 return False

class FileExistsOSError(OSError, metaclass=SpecificOSErrorType):
 errno = 17

a = OSError(17, None)
b = OSError(10, None)
print(a.errno) # >> 17
print(b.errno) # >> 10
print(isinstance(a, FileExistsOSError)) # >> True
print(isinstance(b, FileExistsOSError)) # >> False

raise FileExistsOSError
except OSError as e:
print(e.errno) # >> 17

import os
except FileExistsOSError: # Fails
print("Could not make directory: File already exists")

- No syntax change
- Clean result (as would be expected if the hierarchy was right from the
- Works with any library, and is backwards compatible
- Would enable forward-compatibility (if this was in 3.2, we
could re-implement the 3.3 hierarchy)
- [Half-point] More general use of this may encourage better exception

- It doesn't work yet [*wink*]
- It may be a bad idea to encourage people to override standard
class hierarchies, even if we agree that they are badly designed in 3.2
- Requires use of metaclasses and stuff people shouldn't need to understand
- Setup is extremely ugly and much longer than the replaced code
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20120916/1ac37a5a/attachment.html>

More information about the Python-ideas mailing list