title: How to properly use try/except in Python
body: <p>I have a function that returns the DB connection handler from MongoDB. I have various other functions that makes a call to the DB, I figure let's throw the connection handler into a function so I don't have to define it in every function.</p>
  
  <p>Does this look right? I guess my question is, if it can't make a connection to the DB server, it will print both messages <code>Could not connect to server</code> and <code>No hosts found</code> How can I go about only printing "Could not connect to the server."</p>
  
  <pre><code>def mongodb_conn():
      try:
          conn = pymongo.MongoClient()
      except pymongo.errors.ConnectionFailure, e:
          print "Could not connect to server: %s" % e
      return conn
  
  def get_hosts()
      try:
          conn = mongodb_conn()
          mongodb = conn.dbname.collection
  
          b = []
          hosts_obj = mongodb.find({'_id': 'PR'})
          for x in hosts_obj:
              print x
      except:
          print "No hosts found"
  
  get_hosts()
  </code></pre>