何时使用试试除外? [英] When to use try and except?

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

问题描述

如果我得到零除错误,那么尝试和

是一个糟糕的解决方案,除非它可以用if子句解决。


但是如果程序内存耗尽我应该让它崩溃

对吗?因为如果没有那么我就不得不在各地写例外

防止这种情况发生?


那么我什么时候才能使用try-except?


如果有几个例外,我只想抓住1或2?



试试:

blahaba

除了SomeError:

做点什么

If I get zero division error it is obv a poor solution to do try and
except since it can be solved with an if-clause.

However if a program runs out of memory I should just let it crash
right? Because if not then I''d have to write exceptions everywhere to
prevent that right?

So when would I actually use try-except?

If there can be several exceptions and I just want to catch 1 or 2?
Like
try:
blahaba
except SomeError:
do something

推荐答案

8月29日, 11:23 * am,cnb< circularf ... @ yahoo.sewrote:
On Aug 29, 11:23*am, cnb <circularf...@yahoo.sewrote:

如果我得到零分割错误,那么尝试尝试并不是一个糟糕的解决方案

,因为它可以通过if子句来解决。


但是如果程序内存耗尽我应该让它崩溃

对吗?因为如果没有那么我就不得不在各地写例外

防止这种情况发生?


那么我什么时候才能使用try-except?


如果有几个例外,我只想抓住1或2?



试试:

* * blahaba

除了SomeError:

* *做点什么
If I get zero division error it is obv a poor solution to do try and
except since it can be solved with an if-clause.

However if a program runs out of memory I should just let it crash
right? Because if not then I''d have to write exceptions everywhere to
prevent that right?

So when would I actually use try-except?

If there can be several exceptions and I just want to catch 1 or 2?
Like
try:
* * blahaba
except SomeError:
* * do something



我不是肯定你会尝试做什么,但我认为抓住一个

ZeroDivisionError异常是一个很好的使用try-except。


我是也不确定我会说你只是让程序崩溃如果它b / b
内存不足。我认为从用户的角度来看,你需要检查内存条件并提出异常

表示已经达到一些内存阈值。当提出

异常时,你应该向用户表明并优雅地退出


I''m not sure whay you''re trying to do, but I think catching a
ZeroDivisionError exception is a good use of try-except.

I''m also not sure that I would say you just let a program crash if it
runs out of memory. I would think that from the user perspective, you
would want to check memory conditions and come up with an exception
indicating that some memory threshold has been reached. When that
exception is raised you should indicate that to the user and exit
gracefully.


8月29日,下午1:23,cnb< circularf ... @ yahoo.sewrote:
On Aug 29, 1:23 pm, cnb <circularf...@yahoo.sewrote:

如果我得到零分割错误,那么尝试和

,因为它可以通过if子句来解决。


但是如果程序内存耗尽我应该让它崩溃

对吗?因为如果没有那么我就不得不在各处写例外

防止这样做?


那么我什么时候才能使用try-except?
If I get zero division error it is obv a poor solution to do try and
except since it can be solved with an if-clause.

However if a program runs out of memory I should just let it crash
right? Because if not then I''d have to write exceptions everywhere to
prevent that right?

So when would I actually use try-except?



每当你在except块中做一些有意义的*

处理异常(例如恢复)。有没有任何有意义的行动

当你的内存耗尽时你可以采取什么?如果是(例如,将当前的数据写入磁盘并打开一个弹出窗口通知用户),那么

使用try / except,否则不要。 IOW代码如下


试试:

...

除了MemoryError:

打印你内存不足了!


通常没用;更好的是让异常传播到顶部

级别并带有完整的回溯。


HTH,

George

Whenever you can do something *meaningful* in the except block to
handle the exception (e.g. recover). Is there any meaningful action
you can take when you run out of memory ? If yes (e.g. write current
data to the disk and open a popup window that informs ths user), then
use try/except, otherwise don''t. IOW code like the following

try:
...
except MemoryError:
print "You ran out of memory!"

is typically useless; better let the exception propagate to the top
level with a full traceback.

HTH,
George


8月29日,7:40 * pm,Daniel< daniel.watr ... @ gmail.comwrote:
On Aug 29, 7:40*pm, Daniel <daniel.watr...@gmail.comwrote:

8月29日,11:23 * am,cnb< circularf ... @ yahoo.sewrote:
On Aug 29, 11:23*am, cnb <circularf...@yahoo.sewrote:

如果我得到零分割误差,那么这是一个糟糕的解决方案尝试和

除非因为它可以用if子句解决。
If I get zero division error it is obv a poor solution to do try and
except since it can be solved with an if-clause.


但是如果一个程序内存耗尽我应该让它崩溃

对吗?因为如果没有那么我就不得不在各地写出例外情况

防止这种情况发生?
However if a program runs out of memory I should just let it crash
right? Because if not then I''d have to write exceptions everywhere to
prevent that right?


那么我什么时候才能使用try-except?
So when would I actually use try-except?


如果有几个例外,我只想抓住1或2?



试试:

* * blahaba

除了SomeError:

* *做点什么
If there can be several exceptions and I just want to catch 1 or 2?
Like
try:
* * blahaba
except SomeError:
* * do something



我不确定你想要做什么,但我认为抓住一个

ZeroDivisionError异常是一个很好用的try-except。


我也不确定我会说你只是让程序崩溃如果它b / b
内存不足。 *我认为从用户的角度来看,你需要检查内存条件并提出异常

表示已达到某些内存阈值。 *当提出

异常时,您应该向用户表明并优雅地退出



I''m not sure whay you''re trying to do, but I think catching a
ZeroDivisionError exception is a good use of try-except.

I''m also not sure that I would say you just let a program crash if it
runs out of memory. *I would think that from the user perspective, you
would want to check memory conditions and come up with an exception
indicating that some memory threshold has been reached. *When that
exception is raised you should indicate that to the user and exit
gracefully.



如果使用if子句,最好避免使用ZeroDivisionError,不是吗?b
?这是一个可以预见的例外......


A ZeroDivisionError is better avoided wth an if-clause, don''t you
think? It is a predictable exception...


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

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