Python: how can I catch a exception and continue? Python: how can I catch a exception and continue? json json

Python: how can I catch a exception and continue?


put the try/except inside the loop around the json.JSONEncoder().encode(item):

print "[",lst = [1, 2, 3, JsonedData(u'4'), 5]for i, item in enumerate(lst):    try:        chunk = json.JSONEncoder().encode(item)    except TypeError:         pass    else:        print chunk    finally:        # dont print the ',' if this is the last item in the lst        if i + 1 != len(lst):            print ","print "]"


Use the skipkeys option for JSONEncoder() so that it skips items that it can't encode. Alternatively, create a default method for your JsonedData object. See the docs.