title: Catch Broken Pipe in Python 2 AND Python 3
body: <p>I try to write some code to catch a Broken Pipe Error. The code should run in Python 2.x and Python 3.x.</p>
  
  <p>In Python 2.x a broken pipe is represented by a <code>socket.error</code></p>
  
  <pre><code>socket.error: [Errno 32] Broken pipe
  </code></pre>
  
  <p>This was changed in Python 3.x - a broken pipe now is a <code>BrokenPipeError</code></p>
  
  <pre><code>BrokenPipeError: [Errno 32] Broken pipe
  </code></pre>
  
  <p>Also the syntax of exception handling has changed a bit (see <a href="https://stackoverflow.com/a/34463112/263589">https://stackoverflow.com/a/34463112/263589</a>) so what I need to do would be something like:</p>
  
  <pre><code>try:
      do_something()
  except BrokenPipeError as e: # implies Python 3.x
      resolve_for_python2()
  except socket.error as e:
      if sys.version_info[0] == 2: # this is necessary, as in Python &gt;=3.3
                                   # socket.error is an alias of OSError
                                   # https://docs.python.org/3/library/socket.html#socket.error
          resolve_for_python3()
      else:
          raise
  </code></pre>
  
  <p>There's (at least) one remaining problem: In Python 2.x there is no <code>BrokenPipeError</code>, so whenever there is an exception in <code>do_something()</code> Python 2.x would throw another exception and complain that it doesn't know <code>BrokenPipeError</code>. As <code>socket.error</code> is deprecated in Python 3.x a similar problem could arise in Python 3.x in the near future.</p>
  
  <p>What can I do to make this code run in Python 2.x and Python 3.x?</p>