[Tutor] Scope of exceptions

Alan Gauld alan.gauld at btinternet.com
Wed Aug 5 20:19:51 CEST 2009


"Craig McDonald" <craig.a.mcd at googlemail.com> wrote

> try:
>    statements
>    extractData()
>    more_statements
>    even_more_statements
> except KeyboardInterrupt:
>    exit('Exiting')
> 
> Once it enters/runs the extractData func and if I choose to exit
> (control c) it throws an exception rather than exiting cleanly. Do I
> need to put a 'KeyboardInterrupt' exception for every function?

It should work as you've shown.
What should happen is that if you type Ctrl-C while the script is 
executing extractData() then Python will raise the exception and 
the function will not know what to do with it, so it raises it to the 
next level where your handler should catch it.

So what does your real handler look like?

And what does the exception that is raised look like?

HTH


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list