如何除了意想不到的? [英] How to except the unexpected?

查看:62
本文介绍了如何除了意想不到的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢Java的一件事是需要声明异常

作为接口或类定义的一部分。但也许Java得到了这个

吧...


我写了一个使用urllib2,urlparse,robotparser和
$ b的应用程序$ b电池组中的其他一些模块。有一天,我的应用程序失败了

urllib2.HTTPError。所以我抓住了。但后来我得到了一个urllib2.URLError,所以

我也抓到了。第二天,它遇到了urllib2.HTTPError,然后是一个

IOError,一个socket.timeout,httplib.InvalidURL,......


你怎么样?用这些模块强有力地编程抛出所有那些不同的(有时是无证件的)例外吗?


一个垃圾似乎是一个坏主意,因为它也捕获了属性错误

和程序中的其他错误。


-
$ b $bRenéPijlman

解决方案

Rene Pijlman写道:

我不喜欢Java的一件事是需要声明异常
作为接口或类定义的一部分。但也许Java得到了这个
正确...
我已经写了一个应用程序,它使用urllib2,urlparse,robotparser和
电池组中的其他一些模块。有一天,我的应用程序失败,显示了
urllib2.HTTPError。所以我抓住了。但后来我得到了一个urllib2.URLError,所以
我也抓到了它。第二天,它遇到了urllib2.HTTPError,然后是一个
IOError,一个socket.timeout,httplib.InvalidURL,......

你如何使用这些模块强有力地编程抛出所有这些
有不同的(有时是无证件的)例外吗?

一个垃圾似乎是一个坏主意,因为它还会捕获属性错误和程序中的其他错误。




例如,urllib2的相关行看起来如下:


类URLError(IOError):

class HTTPError(URLError,addinfourl):

class GopherError(URLError):


这表明捕获URLError应该已经捕获了你的HTTPError,

所以你可能会有上面的年表。


例如:


py>类BobError(例外):传递

....

py> class CarolError(BobError):传递

....

py>尝试:

....加注CarolError

....除了BobError:

....打印''得到它''

....

得到它

现在,


%cat httplib.py | grep -e''^ \s * class''


在输出中的某一点产生以下内容:


类HTTPException(例外):

class NotConnected(HTTPException):

class InvalidURL(HTTPException):

class UnknownProtocol(HTTPException):

class UnknownTransferEncoding(HTTPException):

class UnimplementedFileMode(HTTPException):

class IncompleteRead(HTTPException):

class ImproperConnectionState(HTTPException) :

class CannotSendRequest(ImproperConnectionState):

class CannotSendHeader(ImproperConnectionState):

class ResponseNotReady(ImproperConnectionState):

class BadStatusLine(HTTPException):


这表明try:except HTTPException:将足够具体

作为此模块的收藏。


以下,应该抓住你提到的所有内容,除了

套接字超时:


尝试:

无论如何()

除了URLError,HTTPException:

替代方案()


但在我看来,正如你所做的那样与互联网合作无论如何都要充满危险。

James


James Stroud写道:

除了URLError,HTTPException:




Aieee!这只捕获URLError并将名称HTTPException绑定到

该错误的详细信息。除了(URLError,HTTPException)之外,你必须写上



来捕获它们。


-

Ben Caradoc-Davies< be*@wintersun.org>
http://wintersun.org/

那些否认他人自由的人不值得为自己辩护。

- 亚伯拉罕林肯


Ben Caradoc-Davies写道:

James Stroud写道:

除了URLError,HTTPException:



Aieee!这只捕获URLError并将名称HTTPException绑定到该错误的详细信息。你必须写(

除了(URLError,HTTPException):

来捕捉它们。




糟糕。


One of the things I dislike about Java is the need to declare exceptions
as part of an interface or class definition. But perhaps Java got this
right...

I''ve writen an application that uses urllib2, urlparse, robotparser and
some other modules in the battery pack. One day my app failed with an
urllib2.HTTPError. So I catch that. But then I get a urllib2.URLError, so
I catch that too. The next day, it encounters a urllib2.HTTPError, then a
IOError, a socket.timeout, httplib.InvalidURL,...

How do you program robustly with these modules throwing all those
different (and sometimes undocumented) exceptions at you?

A catchall seems like a bad idea, since it also catches AttributeErrors
and other bugs in the program.

--
René Pijlman

解决方案

Rene Pijlman wrote:

One of the things I dislike about Java is the need to declare exceptions
as part of an interface or class definition. But perhaps Java got this
right...

I''ve writen an application that uses urllib2, urlparse, robotparser and
some other modules in the battery pack. One day my app failed with an
urllib2.HTTPError. So I catch that. But then I get a urllib2.URLError, so
I catch that too. The next day, it encounters a urllib2.HTTPError, then a
IOError, a socket.timeout, httplib.InvalidURL,...

How do you program robustly with these modules throwing all those
different (and sometimes undocumented) exceptions at you?

A catchall seems like a bad idea, since it also catches AttributeErrors
and other bugs in the program.



The relevant lines of urllib2, for example, look as such:

class URLError(IOError):
class HTTPError(URLError, addinfourl):
class GopherError(URLError):

This suggests that catching URLError should have caught your HTTPError,
so you might have the chronology backwards above.

E.g.:

py> class BobError(Exception): pass
....
py> class CarolError(BobError): pass
....
py> try:
.... raise CarolError
.... except BobError:
.... print ''got it''
....
got it
Now,

% cat httplib.py | grep -e ''^\s*class''

produces the following at one point in its output:

class HTTPException(Exception):
class NotConnected(HTTPException):
class InvalidURL(HTTPException):
class UnknownProtocol(HTTPException):
class UnknownTransferEncoding(HTTPException):
class UnimplementedFileMode(HTTPException):
class IncompleteRead(HTTPException):
class ImproperConnectionState(HTTPException):
class CannotSendRequest(ImproperConnectionState):
class CannotSendHeader(ImproperConnectionState):
class ResponseNotReady(ImproperConnectionState):
class BadStatusLine(HTTPException):

Which suggests that "try: except HTTPException:" will be specific enough
as a catchall for this module.

The following, then, should catch everything you mentioned except the
socket timeout:

try:
whatever()
except URLError, HTTPException:
alternative()

But it seems to me that working with the internet as you are doing is
fraught with peril anyway.

James


James Stroud wrote:

except URLError, HTTPException:



Aieee! This catches only URLError and binds the name HTTPException to
the detail of that error. You must write

except (URLError, HTTPException):

to catch both.

--
Ben Caradoc-Davies <be*@wintersun.org>
http://wintersun.org/
"Those who deny freedom to others deserve it not for themselves."
- Abraham Lincoln


Ben Caradoc-Davies wrote:

James Stroud wrote:

except URLError, HTTPException:


Aieee! This catches only URLError and binds the name HTTPException to
the detail of that error. You must write

except (URLError, HTTPException):

to catch both.



Oops.


这篇关于如何除了意想不到的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆