idea: add an asynchronous exception class

Paul Rubin http
Sat Mar 4 01:31:49 EST 2006


I'd like to suggest adding a builtin abstract class to Python called
AsynchronousException, which would be a subclass of Exception.  The
only asynchronous exception I can think of right now is
KeyboardInterrupt, so KeyboardInterrupt would become a subclass of
AsynchronousException 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 AsynchronousException.

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 AsynchronousException.  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.



More information about the Python-list mailing list