Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly use try/except in Python

Tags:

python

pymongo

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()
like image 935
sdot257 Avatar asked Dec 11 '22 01:12

sdot257


1 Answers

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.

like image 142
Martijn Pieters Avatar answered Jan 10 '23 19:01

Martijn Pieters