Pythonで次のことを行う方法はありますか?
try:
Thing1()
try_this_too:
Thing2()
try_this_too:
Thing3()
except:
print "Nothing worked :-("
Thing1() が成功した場合、他に何もしたくないことに注意してください。
for thing in (Thing1,Thing2,Thing3):
try:
thing()
break #break out of loop, don't execute else clause
except: #BARE EXCEPT IS USUALLY A BAD IDEA!
pass
else:
print "nothing worked"
これは、任意の数の関数に簡単に拡張できます。
funcs = (Thing1, Thing2, Thing3)
failures = 0
for func in funcs:
try:
func()
break
except Exception:
failures += 1
if failures == len(funcs):
print "Cry evrytime :-("
try:
Thing1()
except:
try:
Thing2()
except:
try:
Thing3()
except:
print "Nothing worked :-("