如果我想打破一个函数,我可以调用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."
Run Code Online (Sandbox Code Playgroud) python ×1