2014-01-18 32 views
6

在代码中有不同的老式类像这样的:如何捕捉python中的所有旧式类异常?

class customException: pass 

和异常升高是这样的:

raise customException() 

有没有一种类型来捕获所有那些旧式类异常?像这样:

try: 
    ... 
except EXCEPTION_TYPE as e: 
    #do something with e 

或者至少有一种方法可以捕捉一切(旧式和新式)并获取变量中的异常对象?

try: 
    ... 
except: 
    #this catches everything but there is no exception variable 
+0

相关:http://python3porting.com/differences.html#except –

回答

3

我能想到的唯一的解决办法是使用sys.exc_info

import sys 
try: 
    raise customException() 
except: 
    e = sys.exc_info()[1] 
    # handle exception "e" here...