**As part of** one of my programs, I want to catch any `MemoryError` and log them. Currently, I'm using `traceback.format_exception` to format all other exceptions; I would like to do that for `MemoryError` as well.

**However**, using `format_exception` requires using more memory, which is exactly what I cannot do when I'm out of memory.

**What do?**

What can I do when I'm thrown a `MemoryError` besides terminating? Ideally, I would log them and then resume the program on the assumption that freeing the part of the stack between the handler and the thrower\* has freed up sufficient memory for me to do so.

(\* and also those heap objects kept alive only by the references emanating from that stack section)