Reputation: 14438
I am working on a framework and writing extensions to it.
The problem is that I only have to raise a particular class of Exception
for the framework.
class FrameworkException(Exception):
I can raise an Exception
which is the a subclass of FrameworkException
and the framework will take care of logging/reporting the exception.
Now, in my plugin I can get a variety of exceptions, let's say a ValueError
. Can I create a FrameworkException
with all the details of ValueError
somehow?
In Java, I will do something like this,
catch (IOException e) {
throw new FrameworkException(e);
}
In this way, all the details of the original exception e
will be preserved including the message and stacktrace like this,
Exception in thread "main" FrameworkException: IOException: Custom Message
at com.Test.main(Test.java:115)
Caused by: IOException: Custom Message
at com.Test.main(Test.java:113)
Is something similar possible in Python?
I tried,
except Exception as e:
raise FrameworkException(e)
But it lost the original traceback and had only the message.
I am using Python 2.7 by the way.
Upvotes: 1
Views: 58
Reputation: 3373
In Python 2 you can raise the your exception with the original traceback, so you shold write:
raise FrameworkException, FrameworkException(e), sys.exc_info()[2]
Or switch to a Python 3
Upvotes: 1