2005年02月22日 星期二 09:26
说的有道理, 在模块级确实是不应该捕获自己不关心且不能处理的异常的. 这也是ftplib中的all_error为什么只定义了几种Exception,而不是把所有的异常都包括的原因. 因为它也是只想处理自己能处理的异常. all_error = ( socket.error, IOError, EOFError, httplib.HTTPException, Exception ) 但是据我观察, cry兄弟的代码已经是业务级的了,甚至可以说是业务逻辑顶层的代码了, 如果此处再不捕获所有错误, 将直接抛出去给操作系统,导致程序异常中止. 另外,我的异常捕获中还有一些代码以下列注释代替了 # do something other 这里包括将异常重新抛出的处理方式, 具体怎么处理,就看具体业务的需要了. ----- Original Message ----- From: "jian wang" <wangjian5748 at 163.com> To: <python-chinese at lists.python.cn> Sent: Monday, February 21, 2005 10:42 PM Subject: Re: Re: Re: [python-chinese]怎么得到httplib的异常呢? > hw_cg,您好! > > 补充一点:千万不要在一个接口中捕捉所有异常,只捕捉此接口(或函数或方法)可以处理的异常, > 对不能合理处理的异常,应该抛出,留给调用者处理,否则和业务有关的具体异常就无法正确处理 > 比如象这样的代码就不好: > except Exception, msg: > print msg > # do something other > 捕捉了所有异常,记住:本级不能合理处理的异常,抛出给调用者处理 > > ======= 2005-02-21 14:21:29 您在来信中写道:======= > > >cry: 你好! > > > >借鉴其他语言的异常思想,不是这样写捕获语句的. > >你的捕获机制不完善,导致你漏掉了一些异常. > > > >建议你的异常捕获象下面这样分级捕获, 每个异常捕获语句仅捕获自己感兴趣的异常, > >最后使用Exception捕获所有未拦截的异常. > > > >import httplib > >import socket > > > >try: > > conn = httplib.HTTPConnection("127.0.0.1:8080") > > conn.request("GET", "/") > > response = conn.getresponse() > > result = response.read() > > print result > > conn.close() > > > >except socket.error, msg: > > print msg > > # do something other > > > >except httplib.HTTPException,msg: > > print msg > > # do something other > > > >except Exception, msg: > > print msg > > # do something other > > > > > > > > > >----- Original Message ----- > >From: "cry" <zyqmail at 163.net> > >To: "limodou, python-chinese at lists.python.cn" <limodou at gmail.com,python-chinese at lists.python.cn> > >Sent: Monday, February 21, 2005 1:26 PM > >Subject: Re: Re: [python-chinese] ÔõôµÃµ½httplib µÄÒì³£ÄØ£¿ > > > > > >limodou,您好! > > > >直接用except:当然是没有问题,但是我需要错误信息呀,比如: > >except error,msg: > > > > > >在 2005-02-21 12:22:00 您写道: > >>捕获Exception 不要httplib.HTTPException 试试 > >> > >> > >>On Mon, 21 Feb 2005 12:5:36 +0800, cry <zyqmail at 163.net> wrote: > >>> python,您好! > >>> > >>> 我做了如下的代码: > >>> try: > >>> conn = httplib.HTTPConnection("192.168.1.103") > >>> conn.request("GET", "/") > >>> response = conn.getresponse() > >>> result = response.read() > >>> conn.close() > >>> except httplib.HTTPException , msg: > >>> print msg > >>> > >>> 运行后: > >>> Traceback (most recent call last): > >>> File "", line 3, in ? > >>> File "I:\Python23\lib\httplib.py", line 718, in request > >>> self._send_request(method, url, body, headers) > >>> File "I:\Python23\lib\httplib.py", line 739, in _send_request > >>> self.endheaders() > >>> File "I:\Python23\lib\httplib.py", line 712, in endheaders > >>> self._send_output() > >>> File "I:\Python23\lib\httplib.py", line 597, in _send_output > >>> self.send(msg) > >>> File "I:\Python23\lib\httplib.py", line 564, in send > >>> self.connect() > >>> File "I:\Python23\lib\httplib.py", line 532, in connect > >>> socket.SOCK_STREAM): > >>> error: (10060, 'Operation timed out') > >>> > >>> 错在那里呢?我怎么能控制httplib的exception呢? > >>> 我这样处理ftplib就可以。 > >>> > >>> 谢谢。 > >>> > >>> 致 > >>> 礼! > >>> > >>> cry > >>> zyqmail at 163.net > >>> > >>> _______________________________________________ > >>> python-chinese list > >>> python-chinese at lists.python.cn > >>> http://python.cn/mailman/listinfo/python-chinese > >>> > >> > >> > >>-- > >>I like python! > >>My Blog: http://www.donews.net/limodou > >>New Maillist: http://groups-beta.google.com/group/python-cn > >>_______________________________________________ > >>python-chinese list > >>python-chinese at lists.python.cn > >>http://python.cn/mailman/listinfo/python-chinese > > > > 致 > >礼! > > > > cry > > zyqmail at 163.net > > > > > >_______________________________________________ > >python-chinese list > >python-chinese at lists.python.cn > >http://python.cn/mailman/listinfo/python-chinese_______________________________________________ > >python-chinese list > >python-chinese at lists.python.cn > >http://python.cn/mailman/listinfo/python-chinese > > > > = = = = = = = = = = = = = = = = = = = = > > > 致 > 礼! > > > jian wang > wangjian5748 at 163.com > 2005-02-21 > > -------------------------------------------------------------------------------- > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese >
2005年02月22日 星期二 09:33
我也要退定,谢谢!!! "Kitty Y. Li" <yuanli at lucent.com> 收件人: <python-chinese at lists.python.cn> 发件人: 抄送: python-chinese-bounces at lists 主题: Re: [python-chinese] 我要退定 .python.cn 2005-02-22 09:32 请答复 给 python-chinese 我也要退定,谢谢 张宇辉 wrote: > 谢谢 > > 邮件太多 > > 违背本意了 > > 故想退定 > > > ------------------------------------------------------------------------ > *Do You Yahoo!?* > 150万曲MP3疯狂搜,带您闯入音乐殿堂 <http://music.yisou.com> > 美女明星应有尽有,搜遍美图、艳图和酷图 <http://image.yisou.com> > 1G就是1000兆,雅虎电邮自助扩容! > < http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/ > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese _______________________________________________ python-chinese list python-chinese at lists.python.cn http://python.cn/mailman/listinfo/python-chinese
Zeuux © 2025
京ICP备05028076号