Thanks Jerry!<br><br>By the way, I know I can change the way the loops works and bypass this issue :)<br>I just wanted to mention this behavior which I thought interesting and present some corner case (I have worked extensively in testing). <br>
And I agree, probably I should not have used color. Bad on my part.<br><br>Cheers!!<br>Sumod<br><br><div class="gmail_quote">On Thu, May 3, 2012 at 7:48 PM, Jerry Hill <span dir="ltr"><<a href="mailto:malaclypse2@gmail.com" target="_blank">malaclypse2@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">On Thu, May 3, 2012 at 9:57 AM, <<a href="mailto:spawgi@gmail.com">spawgi@gmail.com</a>> wrote:<br>
> Hello all,<br>
><br>
> I have encountered the following scenario.<br>
> Here is the code - on IDLE on Windows XP.<br>
><br>
>>>> while True:<br>
> try:<br>
> number = raw_input("enter number - ")<br>
> print number * number<br>
> except ValueError:<br>
> print "invalid number"<br>
> except:<br>
> print "unspecified exception"<br>
> else:<br>
> print "other conditions"<br>
><br>
><br>
> enter number - 3<br>
> unspecified exception<br>
><br>
> What I noticed is that no matter, what input I give, I cannot exit this<br>
> loop. I have tried control-C, control-D etc. all the keys. So how can I exit<br>
> from this loop?<br>
<br>
</div>You can't, because you've painted yourself into a corner. The bare<br>
"except:" line will catch any exception at all. Including the<br>
KeyboardInterrupt exception that is raised when you hit control-c. If<br>
you must catch unknown exceptions, but still want to allow the<br>
KeyboardInterrupt exception from pressing control-c to exit your<br>
program, then use "except Exception:"<br>
instead of a bare "except:" statement. Since KeyboardInterrupt (and<br>
SystemExit) are not subclasses of Exception, they won't be caught.<br>
<br>
KeyboardInterrupt and SystemExit are subclasses of a class called<br>
BaseException, instead of Exception, for this exact purpose. See more<br>
about python's exception hierarchy here:<br>
<a href="http://docs.python.org/library/exceptions.html" target="_blank">http://docs.python.org/library/exceptions.html</a><br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Jerry<br>
</font></span><div class="HOEnZb"><div class="h5">_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><a href="http://spawgi.wordpress.com" target="_blank">http://spawgi.wordpress.com</a><br>We can do it and do it better.<br>