How to catch an exception in python and get a reference to the exception, WITHOUT knowing the type? -


i'm wondering how can catch any raised object (i.e. type not extend exception), , still reference it.

i came across desire when using jython. when calling java method, if method raises exception, not extend python's exception class, block not catch it:

try:     # call java lib raises exception here except exception, e:     # never entered 

i can this, have no access exception object raised.

try:     # call java lib raises exception here except:     # enter here, there's no reference exception raised 

i can solve importing java exception type , catching explicitly, makes difficult/impossible write generic exception handling wrappers/decorators.

is there way catch arbitrary exception , still reference in except block?

i should note i'm hoping exception handling decorator making usable python projects, not jython projects. i'd avoid importing java.lang.exception because makes jython-only. example, figure can (but haven't tried it), i'd avoid if can.

try:     # function may running jython , may raise java exception except (exception, java.lang.exception), e:     # imagine work, makes code jython-only 

you can reference exceptions using sys module. sys.exc_info tuple of type, instance , traceback.

import sys  try:     # call java lib raises exception here except:     instance = sys.exc_info()[1] 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -