user1869421
user1869421

Reputation: 837

Python handling a TypeError:

I'm getting a:

TypeError: 'NoneType' object has no attribute '__getitem__' 

error as my program is querying my db. The problem is that it then halts my whole program which I don't want happening for the web application.

Instead how could I handle this gracefully, as it must refer to a empty field and I'm not to concerned about it. This is a code fragment:

try:
    if not doc['coordinates']:
        xxxxxxxxx
    else:
        xxxxxxxx

except (ValueError, geocoders.google.GQueryError):
    pass

I've tried including a TypeError in the except(), but that causes problems on the client side.

Thanks

Upvotes: 1

Views: 3513

Answers (1)

Himanshu
Himanshu

Reputation: 2454

How about this :-

if doc:
    try:
        if not doc['coordinates']:
            xxxxxxxxx
        else:
            xxxxxxxx

    except (ValueError, geocoders.google.GQueryError):
        pass

Or this :-

    try:
        if not doc or not doc['coordinates']:
            xxxxxxxxx
        else:
            xxxxxxxx

    except (ValueError, geocoders.google.GQueryError):
        pass

Upvotes: 2

Related Questions