如何查找给定的库函数在Python中抛出的所有异常的列表? [英] How Can I Find a List of All Exceptions That a Given Library Function Throws in Python?

查看:149
本文介绍了如何查找给定的库函数在Python中抛出的所有异常的列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



基本上,在官方python文档中我很难找到异常信息。例如,在目前正在编写的一个程序中,我使用的是关闭libary的移动功能:

  from shutil import move 
move('somefile.txt','/tmp/somefile.txt')

只要我有对/ tmp /的写入权限,只要有足够的磁盘空间,并且满足所有其他要求。



但是,当编写通用代码时通常很难保证这些因素,所以通常使用例外:

 从shutil import move 
try:
move('somefile.txt','/tmp/somefile.txt')
除了:
print'由于某种原因移动失败'

我想实际捕获适当的异常抛出,而不是只捕捉一切,但我根本无法找到大多数python模块抛出的异常列表。有没有办法让我看看给定功能可以抛出哪些异常,为什么?这样,我可以为每个例外做出适当的例子,例如:

  from shutil import move 
try:
move('somefile.txt','/tmp/somefile.txt')
除了PermissionDenied:
打印'没有权限'
除了DestinationDoesNotExist:
print/ tmp /不存在
除了NoDiskSpace:
打印'没有可用的磁盘空间'

回答点可以链接到任何可以链接到某些相关文档,我以某种方式在官方文档中忽略,或提供一个确定的方式来确定哪些功能抛出哪些异常以及为什么。 / p>

谢谢!



更新:从答案看来,真的有以100%的直接方式来确定哪些错误被特定的功能抛出。使用元编程,似乎我可以找出简单的例子并列出一些例外,但这不是一个特别有用或方便的方法。



我想考虑最终将有一些标准来定义每个python函数引发的异常,并且这些信息将被包含在官方文档中。直到那时,我想我只是允许这些异常传递给我的用户错误,因为它似乎是最理想的事情。

解决方案

要扩大Messa,请抓住您所期望的失败模式,您知道如何从中恢复。 Ian Bicking写了一篇文章,解决了一些总体原则Eli Bendersky的注释也是如此。



示例代码的问题是,它不是处理错误,只是对其进行预处理并丢弃它们。你的代码不会知道如何处理一个NameError,除了传递它之外没有太多的应该做,除非你觉得你必须添加细节,那么看一下Bicking的重新加注。



IOError和OSError对于 shutil.move 是合理的可预期的,但不一定是可处理的。而你的函数的调用者希望它移动一个文件,如果Eli写的合同被破坏,可能会自动中断。



抓住你可以修复的东西,装饰并重新提高您期望的但无法解决的问题,并让来电者处理您所期望的内容,即使交易的代码是 main


Sorry for the long title, but it seems most descriptive for my question.

Basically, I'm having a difficult time finding exception information in the official python documentation. For example, in one program I'm currently writing, I'm using the shutil libary's move function:

from shutil import move
move('somefile.txt', '/tmp/somefile.txt')

That works fine, as long as I have write access to /tmp/, there is enough diskspace, and if all other requirements are satisfied.

However, when writing generic code, it is often difficult to guarantee those factors, so one usually uses exceptions:

from shutil import move
try:
    move('somefile.txt', '/tmp/somefile.txt')
except:
    print 'Move failed for some reason.'

I'd like to actually catch the appropriate exceptions thrown instead of just catching everything, but I simply can't find a list of exceptions thrown for most python modules. Is there a way for me to see which exceptions a given function can throw, and why? This way I can make appropriate cases for each exception, eg:

from shutil import move
try:
    move('somefile.txt', '/tmp/somefile.txt')
except PermissionDenied:
    print 'No permission.'
except DestinationDoesNotExist:
    print "/tmp/ doesn't exist"
except NoDiskSpace:
    print 'No diskspace available.'

Answer points go to whoever can either link me to some relevant documentation that I've somehow overlooked in the official docs, or provide a sure-fire way to figure out exactly which exceptions are thrown by which functions, and why.

Thanks!

UPDATE: It seems from the answers given that there really isn't a 100% straight-forward way to figure out which errors are thrown by specific functions. With meta programming, it seems that I can figure out simple cases and list some exceptions, but this is not a particularly useful or convenient method.

I'd like to think that eventually there will be some standard for defining which exceptions are raised by each python function, and that this information will be included in the official documentation. Until then I think I will just allow those exceptions to pass through and error out for my users as it seems like the most sane thing to do.

解决方案

To amplify Messa, catch what you expect are failure modes that you know how to recover from. Ian Bicking wrote an article that addresses some of the overarching principles as does Eli Bendersky's note.

The problem with the sample code is that it is not handling errors, just prettifying them and discarding them. Your code does not "know" what to do with a NameError and there isn't much it should do other than pass it up, look at Bicking's re-raise if you feel you must add detail.

IOError and OSError are reasonably "expectable" for a shutil.move but not necessarily handleable. And the caller of your function wanted it to move a file and may itself break if that "contract" that Eli writes of is broken.

Catch what you can fix, adorn and re-raise what you expect but can't fix, and let the caller deal with what you didn't expect, even if the code that "deals" is seven levels up the stack in main.

这篇关于如何查找给定的库函数在Python中抛出的所有异常的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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