How many try/except is too many in python function -


i have function geocodes address. don't want function die trying catch error , return tuple instead. want differentiate between errors, use try/except in multiple places.

is there such thing many try/except? how optimize function?

here code:

def geocode(address):     js = ''     try:         urlq = urllib.urlencode({'address':address, 'sensor':'false'})     except exception, e:         return (false, "error url-encoding address. error:%s" % e, js, 'failed')     try:         f = urllib2.urlopen(geo_url + urlq)         d = f.read()     except exception, e:         return (false, "error making connection. error:%s" % e, js, 'failed')     #     try:         js = json.loads(d)     except exception, e:         return (false, "error converting json. error:%s" % e, js, 'failed')     return (true, '', js, 'ok') 

catching exception bad idea. want specify error want catch.

try:     ... except urlerror, e:     return (false, "error making connection. error:%s" % e, js, 'failed') except valueerror, e:     return (false, "error converting json. error:%s" % e, js, 'failed') except unicodeencodeerror, e:     return (false, "error unicode formatting. error:%s" % e, js, 'failed') 

also returning tuple indicate error not preferred. consider putting try except in calling function , let error propagate up.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -