[Tutor] try & except
alan.gauld at freenet.co.uk
Tue Aug 8 23:29:10 CEST 2006
> I'm playing around with try: and except: i have a code like this
> wx.MessageBox("Kunde inte läsa käll ...
> So far i get how to use it but i would like to be able to execute
> the try block again after it jumped to the exception.
Yes, thats a common desire, unfortunately it isn't possible,
at least not directly.
One way round this is to put the try inside a function then
call the function inside an outer try, like this:
try: bibl = os.listdir(d)
But even here you only get wo bites at the pie.
If you need more you need to write a loop:
success = False
while not success
success = True
except: print 'oops!'
But I agree a retry option would be nice. But I've never found a
language yet that supports it. (There are some good reasons why
this is the case BTW, to do with the uncertain state of the code
block that you exit.)
Author of the Learn to Program web site
More information about the Tutor