复杂Python程序中的异常处理 [英] exception handling in complex Python programs

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

问题描述

Python为程序员提供了一个非常好且功能完备的异常处理

机制。这很好。但是,例如任何

复杂构造的例外很难正确使用,特别是当

程序变大时。


大多数异常问题并不是特定于Python的问题,但是我有时觉得Python因为使用例外的免费n-

简单方式而使它们变得更加尖锐对于它自己的用途并且允许

用户也这样做。


现在,我更具体地说是什么...当一个程序开始增长时/>
大,我发现自己有点害怕所有可能引发的异常因为运行时检测错误而导致的异常

(Python的动态类型也在这里发挥作用),来自代码使用的
库的例外,以及来自我的低级
类的例外。

Python不允许指定抛出哪些异常(C ++的

功能在函数后添加''throw''方法声明指定

可以抛出的异常),这让我感到茫然 - 应该抓住什么

以及在哪里?哪些错误应该留给

传播?


我试过环顾Python博客圈,但是没有$ />
似乎非常关注这个话题。


为不太一致的帖子道歉,但我怀疑你觉得

也是痛苦的并且能理解我的意思。


Eli


PS有一种常见的情况,一个方法传递一个文件名,用文件(比如读取数据)来做某事。该方法应该捕获open()可能抛出的

错误,还是留给调用者?


P.P.S.条件很棒(Common Lisp的例外)

这里:
http://dlweinreb.wordpress.com/2008/...-really-about/

不是特定于CL的,可以应用于Python的异常。

Python provides a quite good and feature-complete exception handling
mechanism for its programmers. This is good. But exceptions, like any
complex construct, are difficult to use correctly, especially as
programs get large.

Most of the issues of exceptions are not specific to Python, but I
sometimes feel that Python makes them more acute because of the free-n-
easy manner in which it employs exceptions for its own uses and allows
users to do the same.

Now, what do I mean more specifically... When a program starts growing
large, I find myself a bit scared of all the exceptions that might be
thrown: Python''s exceptions as a result of runtime-detection of errors
(Python''s dynamic typing also comes into play here), exceptions from
libraries used by the code, and exceptions from my lower-level
classes.
Python doesn''t allow to specify which exceptions are thrown (C++''s
feature adding ''throw'' after a function/method declaration specifying
the exceptions that can be thrown), and this leaves me at loss - what
should be caught and where ? Which errors should be left to
propagate ?

I''ve tried looking around the Python blogosphere, but there doesn''t
seem to be much concern with this topic.

Apologies for the not-too-coherent post, but I suspect you feel the
pain too and can understand my meaning.

Eli

P.S. There''s a common case where a method is passed a filename, to do
something with a file (say, read data). Should the method catch the
errors possibly thrown by open(), or leave it to the caller ?

P.P.S. There''s a great post on conditions (Common Lisp''s exceptions)
here:
http://dlweinreb.wordpress.com/2008/...-really-about/
Not really CL specific, and can apply to Python''s exceptions.

推荐答案

8月20日,12:19 am,eliben< eli ... @ gmail.comwrote:
On Aug 20, 12:19 am, eliben <eli...@gmail.comwrote:

Python提供了一个相当不错且功能完备的异常处理

机制对于程序员来说。这很好。但是,例如任何

复杂构造的例外很难正确使用,特别是当

程序变大时。


大多数异常问题并不是特定于Python的问题,但是我有时觉得Python因为使用例外的免费n-

简单方式而使它们变得更加尖锐对于它自己的用途并且允许

用户也这样做。


现在,我更具体地说是什么...当一个程序开始增长时/>
大,我发现自己有点害怕所有可能引发的异常因为运行时检测错误而导致的异常

(Python的动态类型也在这里发挥作用),来自代码使用的
库的例外,以及来自我的低级
类的例外。

Python不允许指定抛出哪些异常(C ++的

功能在函数/我之后添加''throw'' thod声明指定

可以抛出的异常),这让我感到茫然 - 应该抓住什么

和哪里?哪些错误应该留给

传播?


我试过环顾Python博客圈,但是没有$ />
似乎非常关注这个话题。


为不太一致的帖子道歉,但我怀疑你觉得

也是痛苦的并且能理解我的意思。


Eli


PS有一种常见的情况,一个方法传递一个文件名,用文件(比如读取数据)来做某事。该方法应该捕获open()可能抛出的

错误,还是留给调用者?


P.P.S.条件很好(Common Lisp'的例外)

这里: http://dlweinreb.wordpress.com/2008/...ns-exceptions- ...

Not真的特定于CL,并且可以应用于Python的异常。
Python provides a quite good and feature-complete exception handling
mechanism for its programmers. This is good. But exceptions, like any
complex construct, are difficult to use correctly, especially as
programs get large.

Most of the issues of exceptions are not specific to Python, but I
sometimes feel that Python makes them more acute because of the free-n-
easy manner in which it employs exceptions for its own uses and allows
users to do the same.

Now, what do I mean more specifically... When a program starts growing
large, I find myself a bit scared of all the exceptions that might be
thrown: Python''s exceptions as a result of runtime-detection of errors
(Python''s dynamic typing also comes into play here), exceptions from
libraries used by the code, and exceptions from my lower-level
classes.
Python doesn''t allow to specify which exceptions are thrown (C++''s
feature adding ''throw'' after a function/method declaration specifying
the exceptions that can be thrown), and this leaves me at loss - what
should be caught and where ? Which errors should be left to
propagate ?

I''ve tried looking around the Python blogosphere, but there doesn''t
seem to be much concern with this topic.

Apologies for the not-too-coherent post, but I suspect you feel the
pain too and can understand my meaning.

Eli

P.S. There''s a common case where a method is passed a filename, to do
something with a file (say, read data). Should the method catch the
errors possibly thrown by open(), or leave it to the caller ?

P.P.S. There''s a great post on conditions (Common Lisp''s exceptions)
here:http://dlweinreb.wordpress.com/2008/...ns-exceptions-...
Not really CL specific, and can apply to Python''s exceptions.



也许我过于简单化(我在这里学习),但是我抓住了所有

异常,否则很难理解为一个用户。用

换句话说,当一个更好的错误信息是有用的时候。


再次,这可能太简单了,无法帮助,但唯一的方法是忽略

据我所知,某些类型的例外情况是抓住它们并且

通过。

例如这忽略了类型错误......


尝试:

somethingBad()

除了TypeError,错误:

通过

除了异常,错误:

引发TypeError(错误)

我想你可以写一个装饰师来做这个,如果你想要它在

功能级别,但这似乎有点宽泛。不应该例外地根据具体情况添加保护和退货信息

确切地说需要它们?


- Rafe

Maybe I am oversimplifying (and I am here to learn), but I catch all
exceptions which otherwise would be hard to understand as a user. In
other words, when a better error message is useful.

Again, this is probably too simple to help, but the only way to ignore
certain types of exceptions, as far as I know, is to catch them and
pass.
e.g. this ignores type errors...

try:
somethingBad()
except TypeError, err:
pass
except Exception, err:
raise TypeError(err)
I suppose you could write a decorator to do this if you want it at the
function level, but that seems a bit to broad. Shouldn''t exceptions be
on a case-by-case basis to add protection and return information
exactly where it is needed?

- Rafe


2008年8月19日星期二下午12:19,eliben< el **** @ gmail.comwrote:
On Tue, Aug 19, 2008 at 12:19 PM, eliben <el****@gmail.comwrote:

Python为程序员提供了一个非常好且功能完备的异常处理

机制。这很好。但是,例如任何

复杂构造的例外很难正确使用,特别是当

程序变大时。


大多数异常问题并不是特定于Python的问题,但是我有时觉得Python因为使用例外的免费n-

简单方式而使它们变得更加尖锐对于它自己的用途并且允许

用户也这样做。
Python provides a quite good and feature-complete exception handling
mechanism for its programmers. This is good. But exceptions, like any
complex construct, are difficult to use correctly, especially as
programs get large.

Most of the issues of exceptions are not specific to Python, but I
sometimes feel that Python makes them more acute because of the free-n-
easy manner in which it employs exceptions for its own uses and allows
users to do the same.



很多人似乎都有这种恐惧。他们会对待像他们这样的例外情况。

会处理错误代码,试图处理任何可能的情况。

特定的电话。


这个这是错误的做法,它只会导致更脆弱的代码。

只有2个合理的事情要做例外:

1)处理它,通过我的意思是抓住异常知道什么错误

它表示的条件,并采取适当的措施纠正

错误和

2)通过这样其他东西就有机会了。


当你不知道该怎么办来修复它时捕获异常

是一个错误。充其量,它将使调试程序更难(因为

你正在丢失有关错误的上下文信息),最坏的情况是

会给你的程序增加错误。 Javas检查异常的方式鼓励

空或其他无用的异常处理程序是

它们的一个主要问题。


有''有些担心向最终用户提出例外情况。那是'b $ ba用户界面问题,而不是软件质量或工程问题,

并且它可以解决跟踪追溯的顶级处理程序

如果需要,用户无法看到它们。

Lots of people seem to have this fear. They treat exceptions like they
would treat error codes, trying to handle any possible case around any
particular call.

This is the wrong thing to do, and it only leads to more fragile code.
There are only 2 reasonable things to do with an exception:
1) handle it, by which I mean catch the exception knowing what error
condition it signifies, and take an appropriate action to correct the
error and
2) pass it up so something else has a chance at it.

Catching an exception when you don''t know exactly what to do to fix it
is an error. At best, it will make debugging a program harder (because
you''re losing context information about the error) and at worst it
adds bugs to your program. The way Javas checked exceptions encourage
empty or otherwise useless exception handlers is a major problem with
them.

There''s some fear about presenting exceptions to the end user. That''s
a user interface issues, not a software quality or engineering issue,
and it''s resolvable with top-level handlers that log tracebacks
somewhere a user can''t see them if desired.


现在,我更具体地说是什么......当一个程序开始增长

大,我发现自己有点害怕可能抛出的所有异常因为运行时检测错误而导致Python异常

(Python的动态类型也在这里发挥作用),来自代码使用的
库的例外,以及来自我的低层的异常

classes。

Python不允许指定抛出哪些异常(C ++的

功能在函数/方法声明后添加''throw''指定

可以抛出的异常),这让我感到茫然 - 应该抓住什么

和w这里 ?哪些错误应该留给

传播?
Now, what do I mean more specifically... When a program starts growing
large, I find myself a bit scared of all the exceptions that might be
thrown: Python''s exceptions as a result of runtime-detection of errors
(Python''s dynamic typing also comes into play here), exceptions from
libraries used by the code, and exceptions from my lower-level
classes.
Python doesn''t allow to specify which exceptions are thrown (C++''s
feature adding ''throw'' after a function/method declaration specifying
the exceptions that can be thrown), and this leaves me at loss - what
should be caught and where ? Which errors should be left to
propagate ?



你应该抓住任何可以纠正的东西。如果你没有针对特定例外的特定答案,那就不要抓住它了。

You should catch anything that you can correct. If you don''t have a
specific answer for a specific exception, don''t catch it.


我已经试图环顾Python博客圈,但是没有b $ b似乎对这个话题非常关注。


为不太一致的道歉发帖,但我怀疑你也觉得

太痛苦了,能理解我的意思。


Eli


PS有一种常见的情况,一个方法传递一个文件名,用文件(比如读取数据)来做某事。该方法是否应该捕获open()可能抛出的

错误,或者将其留给调用者?
I''ve tried looking around the Python blogosphere, but there doesn''t
seem to be much concern with this topic.

Apologies for the not-too-coherent post, but I suspect you feel the
pain too and can understand my meaning.

Eli

P.S. There''s a common case where a method is passed a filename, to do
something with a file (say, read data). Should the method catch the
errors possibly thrown by open(), or leave it to the caller ?



适用相同的规则。唯一的异常(没有双关语)是

有时你想重新加注作为一种不同类型的异常。如果你这样做,请确保保留所有原始信息(包括

原始追溯)。

Same rules apply. The only sort-of exception (no pun intended) is that
sometimes you want to re-raise as a different type of exception. Make
sure that you preserve all of the original information (including the
original traceback) if you do this.


PPS条件很棒(Common Lisp的例外)

这里:
http://dlweinreb.wordpress.com/2008/...-really-about/

不是特定于CL的,可以应用于Python的例外。

-
http://mail.python.org/mailman/listinfo/python-list


Rafe写道:
Rafe wrote:

再次,这可能太简单了,无法帮助,但唯一的方法是忽略

据我所知,某些类型的例外情况是捕获它们并且

通过。

例如这忽略了类型错误......


尝试:

somethingBad()

除了TypeError,错误:

通过

除了异常,错误:

引发TypeError(错误)
Again, this is probably too simple to help, but the only way to ignore
certain types of exceptions, as far as I know, is to catch them and
pass.
e.g. this ignores type errors...

try:
somethingBad()
except TypeError, err:
pass
except Exception, err:
raise TypeError(err)



那么什么样的代码是你写的*类型错误*不是

被认为是编程错误? (抓住他们并继续进行是一件事,但抓住他们并忽略他们?)


如果我在一个人身上找到它,我真的很担心一段源代码我不得不

维护。


< / F>

so what kind of code are you writing where *type errors* are not
considered programming errors? (catching them and proceeding is one
thing, but catching them and ignoring them?)

I''d be really worried if I found that in a piece of source code I had to
maintain.

</F>


这篇关于复杂Python程序中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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