title: How to handle exceptions in python using the `with` statement in python?
body: <p>Assuming this piece of code:</p>
  
  <pre><code>connection = get_some_connection() # maybe from oursql
  with connection.cursor() as cursor:
      cursor.execute('some query')
  </code></pre>
  
  <p>I understand that after finishing <code>cursor.close()</code> will be executed automatically. What about exceptions? Must I put them inside?</p>
  
  <pre><code>connection = get_some_connection() # maybe from oursql
  with connection.cursor() as cursor:
      try:
          cursor.execute('some query')
      except IntegrityError, e:
          # handle exceoption
  </code></pre>
  
  <p>Or is there a nicer way to handle them with the with statement?</p>