[Tutor] Please critique my Fraq.py (original) (raw)
Alan Gauld alan.gauld at blueyonder.co.uk
Sun Jul 25 19:10:07 CEST 2004
- Previous message: [Tutor] Please critique my Fraq.py
- Next message: [Tutor] Please critique my Fraq.py
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
>Why does raising SystemExit not do it? >In particular if you move all cleanup code - closing files etc >into a try/finally block the exception route is the preferred method.
How about Brian van den Broek's suggestion of about an hour ago, of using sys.exit(). That does what I was after,
Raising SystemExit is exactly the same, thats all sys.exit() does...
if I execute at the XP command line. With IDLE, it doesn't.
That's because IDLE catches SystemExit. It quite reasonably assumes that you don't want to restart IDLE every time you run your program to an exit.
>Nope, an exception is the only reliable way to jump out of nested >loops.
Could you explain what you mean by reliable? For the problem I posed, one answer is to use sys.exit().
As I say calling sys.exit() is just another way of raising the SystemExit exception. Try this:
import sys try: sys.exit() except SystemExit: print "Told you so!"
>class LoopExit(exception): pass > >try: > while True: > while True: > try: raise LoopBreak > finally: print "Done!" >except LoopBreak:
except LoopExit: # Oops twas a bug...
> print "I escaped!"
I'll give your suggestion a try, but I don't understand classes yet.
The only bit of classes there is the first line, and in fact you don't really need that, I just wanted to show that you could define your own exception type to distinguish between it and SystemExit. I could just have done:
try: while True: while True: try: raise SystemExit finally: print "Done!" except SystemExit: print "I escaped!"
HTH,
Alan G.
- Previous message: [Tutor] Please critique my Fraq.py
- Next message: [Tutor] Please critique my Fraq.py
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]