This is a continuation of my question. Hang a Python script using SQLAlchemy and multiprocessing . As discussed in this question, etching exceptions are problematic in Python. This is usually not a problem, but in one case when errors occur in the python multiprocessing module. Since multiprocessing moves objects around by etching, if an error occurs in the multiprocessing process, the whole process may freeze, as shown in this question.
One possible approach is to fix all problematic exceptions, as discussed in this question. This is not easy, as it is not easy to know in advance what exceptions can be called. An alternative approach proposed by lbolla in answering the question is to catch an exception, build an equivalent harmless exception, and then reconstruct. However, I do not know exactly how to do this. Consider the following code.
class BadExc(Exception): def __init__(self, message, a): '''Non-optional param in the constructor.''' Exception.__init__(self, message) self.a = a import sys try: try:
This code smoothes and unpacks the exception and then throws it throwing an error
raising error Traceback (most recent call last): File "<stdin>", line 11, in <module> Exception: BadExc: bad exception error message
Credits for Glenn Maynard answer the question "Internal Exception" (with tracing) in Python? . This has important stuff, namely trace, error message and exception type, so this may be the best thing to do. But ideally, I would like something similar to the original exception, namely
Traceback (most recent call last): File "<stdin>", line 11, in <module> __main__.BadExc: bad exception error message
or, more generally, with the name of the exception in front, not Exception
. Is it possible?
Alternatively, instead of the BadExc
class, you can use the print foo
BadExc
instead, which gives a NameError
. However, this exception does not require special handling.