I'd like to suggest adding a builtin abstract class to Python called
AsynchronousExc eption, which would be a subclass of Exception. The
only asynchronous exception I can think of right now is
KeyboardInterru pt, so KeyboardInterru pt would become a subclass of
AsynchronousExc eption instead of being a direct subclass of Exception.
There's been talk of adding ways of raising asynchronous exceptions
across threads from user code, so those exceptions would also be
subclassed from AsynchronousExc eption.
The reason for wanting this is the common idiom
try:
run_parrot_code () # might raise ParrotException
except ParrotException :
pass # or do some stuff
except IOError:
pass
except:
# run_parrot_code raised something unexpected
print 'unexpected exception'
raise
The catchall except: block captures not only unexpected exceptions
raised from the dynamic scope of the try: block, but also any
asynchronous exception that might be raised. It should have a way to
distinguish between those two cases, i.e. by checking whether the
caught exception is an instance of AsynchronousExc eption. That's
better than checking for all known asynchronous exceptions explicitly
and then having the code break when a new such exception gets added.
Catchall "except" blocks are generally frowned on because of this
issue but they're used all over the place anyway.
The suggestion above is extremely simple to implement but there might
be better ways to solve the problem.