简体   繁体   中英

How to catch all old-style class exceptions in python?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM