Kent Johnson wrote: > David Bear wrote: > >>I'm attempting to use the cgi module with code like this: >> >>import cgi >>fo = cgi.FieldStorage() >># form field names are in the form if 'name:part' >>keys = fo.keys() >>for i in keys: >> try: >> item,value=i.split(':') >> except NameError, UnboundLocalError: >> print "exception..." >> item,value=(None,None) >>return(item,value) >> >>However, the except block does not seem to catch the exception and an >>unboundlocalerror is thrown anyway. What am I missing? >> > > I don't know why you would get an UnboundLocalError from the above code, > but the correct syntax is > except (NameError, UnboundLocalError):
One possibility is that there are no keys. Then the loop finishes without ever setting item or values. This would give an unbound local error. I assume that the OP would have noticed that in the traceback, but perhaps he missed it. OT, using i for the field namelike that makes my head hurt. Perhaps "for fieldname in fo.keys():" would be in order instead. Regards, -tim > > Your code is binding the actual exception to the name "UnboundLocalError" > > Kent -- http://mail.python.org/mailman/listinfo/python-list