'Catch unhandled exception, log it and store it to be printed at teardown time too.'

def catch_exception(self, type, value, tb):

    self.exc_counter += 1
    def repr_(value):
        try:
            return repr(value)
        except:
            return '<Error    while    REPRing    value>'
    self.last_exc = repr_(value)
    self._register_exception_line('Unhandled    exception    raised    while    running    the    test:    %s    %s', type, self.last_exc)
    stack = []
    while tb:
        stack.append(tb.tb_frame)
        tb = tb.tb_next
    self._register_exception_line('Locals    by    frame,    innermost    last:')
    for frame in stack:
        self._register_exception_line('%s:%s    %s:', frame.f_code.co_filename, frame.f_lineno, frame.f_code.co_name)
        for (key, value) in frame.f_locals.items():
            value = repr_(value)
            if (len(value) > 500):
                value = (value[:500] + '...')
            self._register_exception_line('|    %12s    =    %s', key, value)
