如何在Python 2.5中为局部变量分配异常?

Jos*_*ver 7 python python-2.5

在Python 2.6+中,您可以处理以下异常:

  try:
    # stuff
  except Exception as e:
    return 'exception %s' % type(e)
Run Code Online (Sandbox Code Playgroud)

2.5中的等价物是多少?

Céd*_*ien 11

像这样 :

try:
    # stuff
except Exception, e:
  return 'exception %s' % type(e)
Run Code Online (Sandbox Code Playgroud)

  • [这里](http://docs.python.org/release/3.0.1/whatsnew/2.6.html#pep-3110)为什么. (2认同)
  • 我不认为你需要调用`str()`,因为你已经使用`%s`进行插值,''异常%s'%type(e)`将正常工作 (2认同)