调用者如何知道在Python中执行callee期间是否引发了异常?

时间:2015-08-27 07:37:49

标签: python python-2.7 exception

def foo():
   try:
      do_something()
   except:
      raise
      handle exception

def bar():
   try:
      foo()
   except:
      print 'Error occurred in foo()'

bar将如何知道foo引发了例外情况?

此外,如果出现以下代码的输出:

def foo():
    try:
        print 1/0
    except Exception:
        return  'Caught in foo'

try:
    print 'FOOO'
    print foo()
except Exception:
    print 'Caught in main'

OUTPUT:FOOO \ n抓到了foo

try:
    print 'FOOO'
    print foo()
except Exception:
    print 'Caught in main'


def foo():
    try:
        print 1/0
    except Exception:
        return  'Caught in foo'

OUTPUT:FOOO \ n陷入主

为什么两种情况下的输出都不同?

0 个答案:

没有答案