Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 557
GetFree Avatar asked Jan 18 '14 23:01

GetFree


1 Answers

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...
like image 186
shx2 Avatar answered Oct 31 '22 09:10

shx2