<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _thread_error(self, thread):
 """ Handles the case that the send or receive thread exit or throw an exception. """


        # Avoid double-killing the thread
        if thread == self._send_thread:
            self._send_thread = None
        if thread == self._recv_thread:
            self._recv_thread = None

        # Figure out why the thread exited
        if thread.successful():
            exception = socket.error('thread exited prematurely')
        elif isinstance(thread.exception, gevent.GreenletExit):
            # Thread was killed; don't do anything but close
            self.close()
            return
        else:
            exception = thread.exception

        # Close the connection...
        self.close()

        # Notify the application what happened
        self.closed(exception)