You are viewing an older version (9.10.0). Go to latest (9.13.0)
{ } Raw JSON

bundles / IPython 9.10.0 / IPython / core / interactiveshell / InteractiveShell / set_custom_exc

function

IPython.core.interactiveshell:InteractiveShell.set_custom_exc

source: /IPython/core/interactiveshell.py :1965

Signature

def   set_custom_exc ( exc_tuple handler )

Summary

Set a custom exception handler, which will be called if any of the exceptions in exc_tuple occur in the mainloop (specifically, in the run_code() method).

Parameters

exc_tuple : tuple of exception classes

A tuple of exception classes, for which to call the defined handler. It is very important that you use a tuple, and NOT A LIST here, because of the way Python's except statement works. If you only want to trap a single exception, use a singleton tuple

exc_tuple == (MyCustomException,)
handler : callable

handler must have the following signature

def my_handler(self, etype, value, tb, tb_offset=None):
    ...
    return structured_traceback

Your handler must return a structured traceback (a list of strings), or None.

This will be made into an instance method (via types.MethodType) of IPython itself, and it will be called if any of the exceptions listed in the exc_tuple are caught. If the handler is None, an internal basic one is used, which just prints basic info.

To protect IPython from crashes, if your handler ever raises an exception or returns an invalid result, it will be immediately disabled.

Notes

WARNING: by putting in your own exception handler into IPython's main execution loop, you run a very good chance of nasty crashes. This facility should only be used if you really know what you are doing.

Aliases

  • IPython.InteractiveShell.set_custom_exc