title: Why there is no variable ' not defined' error if I could connect successfully in Python
body: <p>I wrote code to connect to MySQL, and if I connect successfully it works well like the following:</p>
  <pre><code>from mysql import connector
  
  try:
      conn = connector.connect(host=&quot;localhost&quot;, database=&quot;world&quot;, user=&quot;root&quot;, password=&quot;rightPWD&quot;)
      print(conn.is_connected())
  
  except connector.Error as err:
      print(err)
  finally:
      conn.close()
  </code></pre>
  <p>But if I intentionally use the wrong password:</p>
  <pre><code>try:
      conn = connector.connect(host=&quot;localhost&quot;, database=&quot;world&quot;, user=&quot;root&quot;, password=&quot;wrongPWD&quot;)
      print(conn.is_connected())
  
  except connector.Error as err:
      print(err)
  finally:
      conn.close()
  </code></pre>
  <p>There I will get an error:</p>
  <pre><code>1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  Traceback (most recent call last):
    File &quot;/home/Nico/pycharm/plugins/python/helpers/pydev/pydevd.py&quot;, line 1483, in _exec
      pydev_imports.execfile(file, globals, locals)  # execute the script
    File &quot;/home/Nico/pycharm/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py&quot;, line 18, in execfile
      exec(compile(contents+&quot;\n&quot;, file, 'exec'), glob, loc)
    File &quot;/home/Nico/Documents/test/mideterm.py&quot;, line 10, in &lt;module&gt;
      conn.close()
  NameError: name 'conn' is not defined
  </code></pre>
  <p>I know in Java a variable shouldn't be defined within a try{} and then use it in finally{}, but in Python, if I do so and connect successfully, there seems nothing goes wrong. but if I failed, the &quot;NameError: name 'conn' is not defined&quot; will jump out.</p>
  <p>what is the mechanism behind it?</p>