[Tutor] exception about "ctrl+c"

Christian Witts cwitts at compuscan.co.za
Mon Jan 9 13:30:19 CET 2012


On 2012/01/09 02:24 PM, daedae11 wrote:
> I want to catch the "ctrl+c" exception. My program is as following. 
> But when I run my script and press "ctrl"+"c", the  program output 
> nothing. I don't know where did I go wrong. Please help me. Thank you!
> def safe_input(prompting):
>     try:
>         return raw_input(prompting);
>     except KeyboardInterrupt, error:
>         print error;
>         return None;
> def main():
>     a = safe_input("input any thing!\n");
>     print a;
> if __name__ == '__main__':
>     main();
> ------------------------------------------------------------------------
> daedae11
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
def safe_input(prompting):
     try:
         return raw_input(prompting)
     except KeyboardInterrupt:
         print 'KeyboardInterrupt Issued'
         return None

That will work as intended, if you had your original `except 
KeyboardInterrupt, error:` and did a `print repr(error)` afterwards you 
will see it does not contain an error message as you perhaps wanted.

Also, Python does not require semi-colons to denote the end-of-line. It 
can be used if you want to have multiple statements on a single line though.

-- 

Christian Witts
Python Developer
//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120109/8ad9de62/attachment.html>


More information about the Tutor mailing list