Florian Golemo Florian Golemo - 1 month ago 26
Python Question

Jupyter magic to handle notebook exceptions

I have a few long-running experiments in my Jupyter Notebooks. Because I don't know when they will finish, I add an email function to the last cell of the notebook, so I automatically get an email, when the notebook is done.

But when there is a random exception in one of the cells, the whole notebook stops executing and I never get any email. So I'm wondering if there is some magic function that could execute a function in case of an exception / kernel stop.

Like

def handle_exception(stacktrace):
send_mail_to_myself(stacktrace)


%%in_case_of_notebook_exception handle_exception # <--- this is what I'm looking for


The other option would be to encapsulate every cell in try-catch, right? But that's soooo tedious.

Thanks in advance for any suggestions.

Answer

A such magic command does not exist, but you can write it.

from IPython.core.magic import register_cell_magic

@register_cell_magic
def handle(line, cell):
    try:
        exec(cell)
    except Exception as e:
        send_mail_to_myself(e)

It is not possible to load automatically the magic command for the whole notebook, you have to add it at each cell where you need this feature.

%%handle

some_code()
raise ValueError('this exception will be caught by the magic')

You have to add this magic command in every cell

Comments