title: python exception handling inside with block
body: <p>Is the following code doing anything wrong with <code>with</code> statement and exception handling of python3? If no, what is the correct way of writing my expected output?</p>
  
  <pre><code>from contextlib import contextmanager
  
  @contextmanager
  def test():
      print("Hello")
      yield
      print("goodbye")
  
  try:
      with test():
          print("inside test")
          raise KeyError
  except KeyError:
      print("KeyError")
  else:
      print("else")
  finally:
      print("finally")
  </code></pre>
  
  <p>and the output is</p>
  
  <pre><code>Hello
  inside test
  KeyError
  finally
  </code></pre>
  
  <p>and I <strong>expect</strong> the output is:</p>
  
  <pre><code>Hello
  inside test
  goodbye
  KeyError
  finally
  </code></pre>
  
  <p>which I believe <a href="https://stackoverflow.com/questions/713794/catching-an-exception-while-using-a-python-with-statement">other people</a> write similarly in the hope that the file would be closed when an exception was raised during the processing of the file.</p>
  
  <p>My python3 version is:</p>
  
  <pre><code>Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
  [GCC 5.4.0 20160609] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  &gt;&gt;&gt; import sys
  &gt;&gt;&gt; print(sys.version)
  3.5.2 (default, Nov 23 2017, 16:37:01) 
  [GCC 5.4.0 20160609]
  </code></pre>