def set_custom_exc(self, exc_tuple, handler):
assert type(exc_tuple)==type(()) ,"<STR_LIT>"<EOL>def dummy_handler(self,etype,value,tb,tb_offset=None):<EOL><INDENT>print('<STR_LIT>')<EOL>print('<STR_LIT>',etype)<EOL>print('<STR_LIT>',value)<EOL>print('<STR_LIT>',tb)<EOL><DEDENT>def validate_stb(stb):<EOL><INDENT>"""<STR_LIT>"""<EOL>msg = "<STR_LIT>" % stb<EOL>if stb is None:<EOL><INDENT>return []<EOL><DEDENT>elif isinstance(stb, str):<EOL><INDENT>return [stb]<EOL><DEDENT>elif not isinstance(stb, list):<EOL><INDENT>raise TypeError(msg)<EOL><DEDENT>for line in stb:<EOL><INDENT>if not isinstance(line, str):<EOL><INDENT>raise TypeError(msg)<EOL><DEDENT><DEDENT>return stb<EOL><DEDENT>if handler is None:<EOL><INDENT>wrapped = dummy_handler<EOL><DEDENT>else:<EOL><INDENT>def wrapped(self,etype,value,tb,tb_offset=None):<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>stb = handler(self,etype,value,tb,tb_offset=tb_offset)<EOL>return validate_stb(stb)<EOL><DEDENT>except:<EOL><INDENT>self.set_custom_exc((), None)<EOL>print("<STR_LIT>", file=io.stderr)<EOL>stb = self.InteractiveTB.structured_traceback(*sys.exc_info())<EOL>print(self.InteractiveTB.stb2text(stb), file=io.stdout)<EOL>print("<STR_LIT>", file=io.stdout)<EOL>stb = self.InteractiveTB.structured_traceback(<EOL>(etype,value,tb), tb_offset=tb_offset<EOL>)<EOL><DEDENT>return stb<EOL><DEDENT><DEDENT>self.CustomTB = types.MethodType(wrapped,self)<EOL>self.custom_exceptions = exc_tuple<EOL>
set_custom_exc(exc_tuple,handler)

        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.

        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.