[Python-Dev] Proposal - KeyboardInterrupt should inherit directly from Exception

Greg Ward gward@python.net
Wed, 7 Nov 2001 08:33:49 -0500


On 07 November 2001, Skip Montanaro said:
> I have a simple proposal: Change the exception class hierarchy
> slightly, so that exceptions you generally will want to re-raise don't
> inherit from StandardError.  Currently, SystemExit, StopIteration and
> Warning inherit directly from Exception.  I suggest that
> KeyboardInterrupt should also inherit from Exception, and not
> StandardError.

Sounds sensible to me.

> That way, the standard catch all except clause can be
> 
>     try:
>         fragile code
>     except StandardError:
>         recover

So "fragile code" would be the main loop of a GUI or server, or an eval
or exec of user-supplied code (eg. a config file that happens to be
Python source) -- that sort of thing?  Those are the only legitimate
places that I can think of "except: ...", and changing that to
"except StandardError: ..." seems like a tiny hardship that buys a
little something.

Hmmm... does anyone else habitually write

  if __name__ == "__main__":
      try:
          main()
      except KeyboardInterrupt:
          sys.exit("interrupted")

And is anyone else sick of doing this?

        Greg
-- 
Greg Ward - nerd                                        gward@python.net
http://starship.python.net/~gward/
There are no stupid questions -- only stupid people.