首页 > 其他 > 详细

处理异常的两种写法

时间:2017-04-23 15:44:06      阅读:154      评论:0      收藏:0      [点我收藏+]
 1 from urllib.request import Request, urlopen
 2 from urllib.error import URLError, HTTPError
 3 req = Request(someurl)
 4 try:
 5     response = urlopen(req)
 6 except HTTPError as e:
 7     print(The server couldn\‘t fulfill the request.)
 8     print(Error code: , e.code)
 9 except URLError as e:
10     print(We failed to reach a server.)
11     print(Reason: , e.reason)
12 else:
13 # everything is fine
 1 from urllib.request import Request, urlopen
 2 from urllib.error import URLError
 3 req = Request(someurl)
 4 try:
 5     response = urlopen(req)
 6 except URLError as e:
 7     if hasattr(e, reason):
 8         print(We failed to reach a server.)
 9         print(Reason: , e.reason)
10     elif hasattr(e, code):
11         print(The server couldn\‘t fulfill the request.)
12         print(Error code: , e.code)
13 else:
14 # everything is fine

 

处理异常的两种写法

原文:http://www.cnblogs.com/themost/p/6752596.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!