문제

In a code where there are different old-style classes like this one:

class customException: pass

and exceptions are raised this way:

raise customException()

Is there a type to catch all those old-style class exceptions? like this:

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

Or at least is there a way to catch everything (old- and new-style) and get the exception object in a variable?

try:
    ...
except:
    #this catches everything but there is no exception variable 
도움이 되었습니까?

해결책

The only solution I can think of is using sys.exc_info

import sys
try:
    raise customException()
except:
    e = sys.exc_info()[1]
    # handle exception "e" here...
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top