:Pythonの:トライ以外式Iは、httpリクエストにそれを適用しようとすると、常にデフォルト値を取得し、私は仲間として、いくつかのGoogleのAPIを使用し
def get_address(lat, lng):
url = "https://maps.googleapis.com/maps/api/geocode/json?{}".\
format(urllib.parse.urlencode(args))
...
try:
r = requests.get(url)
...
return r
except OSError as e:
raise NetException(e.message, 400)
私は、try-例外がexcption場合を扱う使ってみながら、ネットワークエラー。 the try-exception Expression is from here
def try_except(success, failure, *exceptions):
try:
return success()
except exceptions or Exception:
return failure() if callable(failure) else failure
しかし、私は単に成功の機能を実行する場合、私は成功した結果を取得した場合でも、私は常にHTTPの故障reultsを取得し、これらの例外を使用するようにしてください。 httpリクエストのonlly failtureが失敗結果を得る一方で、成功した結果が正しい文字列reusltになるだろうことを確認する方法
>>> re=get_address(-33.865, 151.2094)
>>> re
'Sydney'
>>> r=try_except(get_address(-33.865, 151.2094),"")
>>> r
''
?
だろう。すべての 'Exception'をキャッチして、何が起こっているのかを正確に伝えていた非常に有用な例外メッセージを隠しています。 – user2357112