2005年02月23日 星期三 09:36
我觉得jian wang 说的还是挺有理的。。 >>>> 借鉴其他语言的异常思想,不是这样写捕获语句的. >>>> 你的捕获机制不完善,导致你漏掉了一些异常. >>>> >>>> 建议你的异常捕获象下面这样分级捕获, 每个异常捕获语句仅捕获自己感兴趣的异常, >>>> 最后使用Exception捕获所有未拦截的异常. 即使在业务层面也应该这样来处理吧。。。 On 2/22/05 7:47 PM, "jian wang" <wangjian5748 at 163.com> wrote: > hw_cg,您好! > > 我只是想提醒大家这个关键知识点,主要是针对没有项目经验的学生朋友.包括部分做项目的朋友!并不 > 想那这段代码来说事!! > 因为不少人会说自己的代码不会抛出任何异常,请别人的放心使用的蠢话. 我真的遇到不少,不少还是老 > 手!! > > ======= 2005-02-22 09:26:18 您在来信中写道:======= > >> 说的有道理, 在模块级确实是不应该捕获自己不关心且不能处理的异常的. >> 这也是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] ????μ?μ1/2httplib μ??ì3£??£? >>>> >>>> >>>> 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 >>> _______________________________________________ >> python-chinese list >> python-chinese at lists.python.cn >> http://python.cn/mailman/listinfo/python-chinese >> > > = = = = = = = = = = = = = = = = = = = = > > > 致 > 礼! > > > jian wang > wangjian5748 at 163.com > 2005-02-22 > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese
Zeuux © 2025
京ICP备05028076号