如果我想突破某個功能,我可以撥打return
。如何突破父母功能?
如果我在一個子函數中,並且想要跳出調用子函數的父函數,該怎麼辦?有沒有辦法做到這一點?
小例子:
def parent():
print 'Parent does some work.'
print 'Parent delegates to child.'
child()
print 'This should not execute if the child fails(Exception).'
def child():
try:
print 'The child does some work but fails.'
raise Exception
except Exception:
print 'Can I call something here so the parent ceases work?'
return
print "This won't execute if there's an exception."
引發異常並讓父函數捕獲它。你不能沒有家長的監督; – zondo