Overriding a built-in exception handler
programmer.py at gmail.com
Thu Jul 21 17:15:50 CEST 2005
You can't override an exception. You can only catch whatever
exception is thrown.
For your case, you would want to wrap that while loop up in a
try/catch block like this:
print "Yay for me!"
print "CTRL-C caught"
Someone had mentioned possibly overriding sys.excepthook, but that
doesn't really "override" an exception handler. That function is
called when an unhandled exception occurs. That little hook is really
nice if you want to display information back to the user and possibly
report the info back to a server somewhere.
On 21 Jul 2005 07:39:10 -0700, callmebill at gmail.com
<callmebill at gmail.com> wrote:
> I'm having a tough time figuring this one out:
> class MyKBInterrupt( ..... ):
> print "Are you sure you want to do that?"
> if __name__ == "__main__":
> while 1:
> print "Still here..."
> So this thing keeps printing "Still here..." until the user hits ctl-c,
> at which time the exception is passed to MyKBInterrupt to handle the
> exception, rather than to whatever the built-in handler would be.
> I've Read-TFM, but I only see good info on how to create my own class
> of exception; I don't see anything on how to override an existing
> exception handler.
> Thanks in advance for any help.
More information about the Python-list