2

我用 urlfetch 测试人们提供的 URL 以捕获错误的链接。

result = urlfetch.fetch(url)

当我提供诸如 «http://qwerty.uiop» 之类的 URL 时,日志显示存在 «DNSLookupFailedError»,但此代码无法捕获它:

except urlfetch.DNSLookupFailedError:
self.error(400)
self.response.out.write(
  'Sorry, there was a problem with URL "' + url + '"')

我还尝试了“除了 urlfetch.Error:”和“除了 urlfetch.DownloadError:”

我做错了什么,还有另一种方法可以完成我想做的事情吗?

4

1 回答 1

2

在本地开发人员环境和生产环境中,我实际上看到了一个不同的异常:DownloadError. 捕捉对我来说效果很好。

try:
  result = urlfetch.fetch('http://qwerty.uiop')
except urlfetch.DownloadError:
  self.response.write('Oops!')
于 2012-11-10T02:39:16.040 回答