简体   繁体   中英

How to properly use try/except in Python

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.

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 Could not connect to server and No hosts found How can I go about only printing "Could not connect to the server."

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()

Move your conn = mongodb_conn() call out of the try .. except handler, and test if None was returned:

def get_hosts()
    conn = mongodb_conn()
    if conn is None:
        # no connection, exit early
        return

    try:
        mongodb = conn.dbname.collection

        b = []
        hosts_obj = mongodb.find({'_id': 'PR'})
        for x in hosts_obj:
            print x
    except:
        print "No hosts found"

You should, at all cost, avoid using a blanket except however; you are catching everything now, including memory errors and keyboard interrupts, see Why is "except: pass" a bad programming practice?

Use specific exceptions only ; you can use one except statement to catch multiple exception types:

except (AttributeError, pymongo.errors.OperationFailure):

or you can use multiple except statements handle different exceptions in different ways.

Limit the exception handler to just those parts of the code where the exception can be thrown. The for x in hosts_obj: loop for example is probably not going to throw an AttributeError exception, so it should probably not be part of the try block.

Note that you'll need to adjust your mongodb_conn() function to not try and use the conn local if it has never been set; you'll get an UnboundLocal error if you do:

def mongodb_conn():
    try:
        return pymongo.MongoClient()
    except pymongo.errors.ConnectionFailure, e:
        print "Could not connect to server: %s" % e

Now the function returns the connection if successful, None if the connection failed.

You can also check if the server is available like this:

        from pymongo.errors import ConnectionFailure
        client = MongoClient()
        try:
            # The ismaster command is cheap and does not require auth.
            client.admin.command('ismaster')
        except ConnectionFailure:
            print("Server not available")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM