识别可以引发的异常 [英] Identifying exceptions that can be raised

查看:59
本文介绍了识别可以引发的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,但我在文档或常见问题中找不到任何关于此的内容。我在IDLE菜单中四处寻找但没有看到任何东西。


是否有工具可以确定可以提出的所有异常

在Python函数中,或在它调用的任何函数中,等等?


/ Dan

解决方案

< blockquote> dkcpub在2004-11-18 22:38对世界说:

我是Python的新手,但我在文档或常见问题中找不到任何内容>关于这一点。我在IDLE菜单中四处寻找但没有看到任何东西。

是否有一个工具可以确定在Python函数或任何函数中可以引发的所有异常它所称的功能等等?

/ Dan




嗨丹,

你的意思是存在的所有例外吗?如果是这样的话,虽然我不知道b
Python足以保证其完整性,请查看

< http://www.python.org/doc /2.3.4/lib/module-exceptions.html> ;.


如果你的意思是你想要一个给定函数可以提出的异常列表,b $ b b,好吧,没关系:-)


Best,


Brian vdB

Brian van den Broek写道:

如果你的意思是你想要一个给定函数可以引发的异常列表,那么,没关系: - )




是的,这就是我的意思。我写了foo(),它调用了六个

库函数。 foo()的调用者有什么例外可以处理?
?它有一种自动发现方式吗?


/ Dan


dkcpub写道:

Brian van den Broek写道:

如果你的意思是你想要一个给定函数可以提出的异常列表,那么,没关系: - )



是的,这就是我的意思。我写了foo(),它调用了6个库函数。 foo()的调用者有什么例外可以处理?它有一种自动发现方式吗?

/ Dan




我所知道的。你只需要为这个函数提供典型的数据和

你可以想到的任何极端情况,并处理在这个过程中出现的所有失败。 unittest模块可以帮助你实现自动化。

希望能够获得功能可以提出的一系列全面的异常,这对于Python来说是徒劳无功的。一个小例子可以说明这一点:

def add(a,b):
... 。返回a + b

.... add(1,2)
3 add(1," 2")
Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< stdin>",第2行,添加

TypeError:不支持的操作数类型+:''int''和''str''


上述调用应涵盖我们普通函数的典型用法。那么

是否有意义,记录/报告add()可能会引发TypeError?我认为这会产生误导:

class Int(int):
.... def __add __(self,other):

....返回自我/其他

.... add(Int(1),0)



回溯(最近一次调用最后一次):

文件"< stdin>",第1行,在?

文件"< stdin>" ,第2行,添加

文件"< stdin>",第3行,在__add__

ZeroDivisionError:整数除法或模数为零


这可能看似人为,但对于任何非平凡的功能,

例外的数量是开放式的,并试图通过处理
$ b来实现可靠性$ b固定的异常集基本上是要求函数'的签名的一小部分的类型声明 - 以及它的

有用性的一部分,即使是静态类型的语言(我认为Java有点b $ b,但是C sharp不会)。

Pu你的时间更好用,并写一个好的测试套件。


彼得


I''m very new to Python, but I couldn''t find anything in the docs or faq
about this. And I fished around in the IDLE menus but didn''t see anything.

Is there a tool that can determine all the exceptions that can be raised
in a Python function, or in any of the functions it calls, etc.?

/Dan

解决方案

dkcpub said unto the world upon 2004-11-18 22:38:

I''m very new to Python, but I couldn''t find anything in the docs or faq
about this. And I fished around in the IDLE menus but didn''t see anything.

Is there a tool that can determine all the exceptions that can be raised
in a Python function, or in any of the functions it calls, etc.?

/Dan



Hi Dan,

Do you mean all the exceptions that exist? If so, while I don''t know
Python well enough to vouch for its completeness, check out
<http://www.python.org/doc/2.3.4/lib/module-exceptions.html>.

If you mean you want a list of exceptions that a given function could
raise, well, never mind :-)

Best,

Brian vdB


Brian van den Broek wrote:

If you mean you want a list of exceptions that a given function could
raise, well, never mind :-)



Yeah, that is what I mean. I write foo(), which calls a half-dozen
library functions. What exceptions is a caller of foo() going to have
to handle? It there an automated way of discovering that?

/Dan


dkcpub wrote:

Brian van den Broek wrote:

If you mean you want a list of exceptions that a given function could
raise, well, never mind :-)



Yeah, that is what I mean. I write foo(), which calls a half-dozen
library functions. What exceptions is a caller of foo() going to have
to handle? It there an automated way of discovering that?

/Dan



None that I know of. You just have to feed the function the typical data and
any corner cases that you can think of and handle all failures that appear
in the process. The unittest module can help you automate this.
Hoping to ever get a comprehensive set of exceptions a function can raise is
a vain effort in Python. A little example may illustrate this:

def add(a, b): .... return a + b
.... add(1, 2) 3 add(1, "2") Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in add
TypeError: unsupported operand type(s) for +: ''int'' and ''str''

The above calls should cover the typical usage of our trivial function. Does
it make sense then, to document/report that add() may raise a TypeError? I
think that would be misleading:
class Int(int): .... def __add__(self, other):
.... return self/other
.... add(Int(1), 0)


Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in add
File "<stdin>", line 3, in __add__
ZeroDivisionError: integer division or modulo by zero

This may seem contrived, but for any non-trivial function the number of
exceptions is open-ended, and trying to achieve reliability by handling a
fixed set of exceptions is essentially asking for type declarations for a
small portion of a function''s signature - and a portion where its
usefulness is debated even for statically typed languages (I think Java has
it, but C sharp doesn''t).
Put your time to a better use and write a good test suite.

Peter


这篇关于识别可以引发的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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