Pregunta

In my real case a Segmentation fault arises in the finally clause which I can't do anything about because it stems from an external library used via ctypes. Actually, I don't care about this segfault because the script is done anyway.

However, the segfault in the finally eats all Exceptions occuring prior to it. Thus, debugging that first NameError from iDontExist becomes a pain in the ass. It doesn't occur anywhere. Currently there is no way of seeing any raised Exceptions from before the segfault.

def f1():
    try:
        while True:
            pass
    except KeyboardInterrupt:
        print iDontExist

if __name__=="__main__":
    try:
        f1()
    finally:
        raise Exception("segfault here")
        print "finally"

What do you think I could do about it? Fixing the external library is not an option.

¿Fue útil?

Solución

You can try to catch exceptions before your finally:

try:
    f1()
except NameError as error:  # Change as needed
    print "Error caught:", error  # Or simply "raise", in order to raise the error caught
finally:
    raise Exception("segfault here")
    print "finally"

That said, abamert is right: a segmentation fault is not an exception, so you might be looking for something else.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top