Python风格:异常与sys.exit() [英] Python style: exceptions vs. sys.exit()

查看:86
本文介绍了Python风格:异常与sys.exit()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于Python风格的一般问题,或者可能只是很好的

编程实践。


我的小组正在开发一个中等大小的通用库-purpose

Python函数,其中一些是I / O.因此,许多库函数可能会引发IOError异常。

的问题是:如果库函数能够转发到

sys.exit()并显示有关错误的消息(例如无法打开)这个

文件"),还是应该将异常传播到

处理问题的调用程序?


提前谢谢对于任何可以回答我的问题的人或者

指出我已经回答了这个问题的地方。

I have a general question of Python style, or perhaps just good
programming practice.

My group is developing a medium-sized library of general-purpose
Python functions, some of which do I/O. Therefore it is possible for
many of the library functions to raise IOError Exceptions. The
question is: should the library function be able to just dump to
sys.exit() with a message about the error (like "couldn''t open this
file"), or should the exception propagate to the calling program which
handles the issue?

Thanks in advance for anyone who can either answer my question or
point me to where this question has already been answered.

推荐答案

Drake写道:
Drake wrote:

我有一个Python风格的一般问题,或者可能只是很好

编程实践。


我的团队正在开发一个中等大小的通用工具库,其中一些是I / O.因此,许多库函数可能会引发IOError异常。

的问题是:如果库函数能够转发到

sys.exit()并显示有关错误的消息(例如无法打开)这个

文件"),还是应该将异常传播到

处理问题的调用程序?


提前谢谢对于任何可以回答我的问题的人或者

指出我已经回答了这个问题的地方。
I have a general question of Python style, or perhaps just good
programming practice.

My group is developing a medium-sized library of general-purpose
Python functions, some of which do I/O. Therefore it is possible for
many of the library functions to raise IOError Exceptions. The
question is: should the library function be able to just dump to
sys.exit() with a message about the error (like "couldn''t open this
file"), or should the exception propagate to the calling program which
handles the issue?

Thanks in advance for anyone who can either answer my question or
point me to where this question has already been answered.



恕我直言的图书馆应该总是让异常传播到调用者。

这允许调用者选择采取适当的操作。


-Larry

IMHO libraries should always just let the exception propagate up to the caller.
That allows the caller the option of taking the appropriate action.

-Larry


2008-09-23,Drake< cj ***** @ gmail.comwrote:
On 2008-09-23, Drake <cj*****@gmail.comwrote:

我的小组正在开发一种媒体通用的大型库

Python函数,其中一些是I / O.因此,许多库函数可能会引发IOError异常。

的问题是:如果库函数能够转发到

sys.exit()并显示有关错误的消息(例如无法打开)这个

文件"),
My group is developing a medium-sized library of general-purpose
Python functions, some of which do I/O. Therefore it is possible for
many of the library functions to raise IOError Exceptions. The
question is: should the library function be able to just dump to
sys.exit() with a message about the error (like "couldn''t open this
file"),



编号库模块永远不应该调用sys.exit()。

No. A library module should never call sys.exit().


或者该异常是否应该传播给调用程序

处理问题?
or should the exception propagate to the calling program which
handles the issue?



是的。如果需要,让应用程序处理错误。如果

没有处理,它最终会导致程序退出。


-

格兰特爱德华兹格兰特哇!这是去伯班克市中心的所有

的洞!

visi.com

Yes. Let the application handle the error if it wants to. If
it''s not handled, it''ll end up causing the program to exit.

--
Grant Edwards grante Yow! It''s a hole all the
at way to downtown Burbank!
visi.com




问题是:库函数是否能够转储到

sys.exit()并显示有关错误的消息(例如无法打开这个

文件),或者该异常是否会传播到

处理问题的调用程序?
question is: should the library function be able to just dump to
sys.exit() with a message about the error (like "couldn''t open this
file"), or should the exception propagate to the calling program which
handles the issue?



我的观点是,有例外是为了告诉调用

程序有关错误并给程序员一个机会做什么?b $ b聪明的事情它。一个库,正确地,甚至不知道运行它的

上下文,并且sys.exit()非常繁重

用于要调用的库并显示超出图书馆

应该假设其运行环境的假设。


imho

my view is that the exceptions are there precisely to tell the calling
program about the error and give the programmer a chance to do
something smart about it. A library, properly, doesn''t even know the
context in which it is running, and sys.exit() is pretty heavy handed
for a library to call and shows assumptions beyond what a library
should assume about its running environment.

imho


这篇关于Python风格:异常与sys.exit()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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