试试:最后:问题 [英] try: finally: question

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

问题描述

伙计 -


我有一个关于正确的问题。使用try:finally:...考虑

这样的代码如下:


d = Device.open()

试试:

d.someMethodThatCanRaiseError(...)

如果SomeCondition:

raise错误#Error是Exception的子类

d.someMethodThatCanRaiseError(...)

... d上的许多其他方法最终会引发错误



d .close()

当我运行这段代码,并且出现错误时,我得到追溯(我不想要b $ b想要的)并且程序终止(我不想要)。但是,将每个可以将Error引入try的方法输入

:除了:块没有帮助,

因为我需要重新引发Error才能爆发块的最后得到

:被触发:

d = Device.open()

试试:

尝试:

d.someMethodThatCanRaiseError(...)

除了错误:

#在这里做点什么

引发错误

如果SomeCondition:

raise错误#Error是Exception的子类

尝试:

d。 someMethodTanCanRaiseError(...)

除了错误:

#在这里做点什么

加注错误


... d上有很多其他方法可以最终提高错误



d.close()

我几乎觉得我需要一个goto声明!我最后错过了一些关于

的东西,还是在实践中它真的不是很有用的?


谢谢,


-Don

Hi, Folks-

I have a question regarding the "proper" use of try: finally:... Consider
some code like this:

d = Device.open()
try:
d.someMethodThatCanRaiseError(...)
if SomeCondition:
raise Error # Error is subclass of Exception
d.someMethodThatCanRaiseError(...)
... lots of other methods on d that can raise Error
finally:
d.close()
When I run this code, and an Error is raised, I get traceback (which I don''t
want) and the program terminates (which I don''t want). However, putting in
each method that can raise Error into a try: except: block doesn''t help,
because I need to re-raise Error to break out of the block and get the
finally: to be triggered:
d = Device.open()
try:
try:
d.someMethodThatCanRaiseError(...)
except Error:
# Do something here
raise Error
if SomeCondition:
raise Error # Error is subclass of Exception
try:
d.someMethodThatCanRaiseError(...)
except Error:
# Do something here
raise Error

... lots of other methods on d that can raise Error
finally:
d.close()
I almost feel like I need a goto statement! Am I missing something about
finally:, or is it really not altogther very useful in practice?

Thanks,

-Don

推荐答案

djw写道:
djw wrote:
我有一个问题适当的使用try:finally:...考虑一下这样的代码:
[...]
当我运行这段代码,并且出现错误时,我得到追溯(我是不要想要)并且程序终止(我不想要)。
I have a question regarding the "proper" use of try: finally:... Consider
some code like this:
[...]
When I run this code, and an Error is raised, I get traceback (which I don''t
want) and the program terminates (which I don''t want).




如果我误解你是什么我很抱歉想要,但听起来像你好b $ b只需要一个封闭的尝试/除外。


d = Device.open()

尝试:

试试:

d.someMethodThatCanRaiseError(...)

if SomeCondition:

raise error #Error是Exception的子类

d.someMethodThatCanRaiseError(...)

... d上的许多其他方法可以引发错误

终于:

d.close()

除了:

#无论你想要什么

有什么不对那个?



I''m sorry if I misinterpret what you want, but it sounds like you
need nothing more than an enclosing try/except.

d = Device.open()
try:
try:
d.someMethodThatCanRaiseError(...)
if SomeCondition:
raise Error # Error is subclass of Exception
d.someMethodThatCanRaiseError(...)
... lots of other methods on d that can raise Error
finally:
d.close()
except:
# whatever you want here
Is anything wrong with that?


在文章< O6 ******************** @ powergate.ca>,

Peter Hansen< pe *** @ engcorp.com>写道:
In article <O6********************@powergate.ca>,
Peter Hansen <pe***@engcorp.com> wrote:
djw写道:
djw wrote:
我有一个关于正确的问题。使用try:finally:...考虑一下这样的代码:
[...]
当我运行这段代码,并且出现错误时,我得到追溯(我是不想要)并且程序终止(我不想要)。
I have a question regarding the "proper" use of try: finally:... Consider
some code like this:
[...]
When I run this code, and an Error is raised, I get traceback (which I don''t
want) and the program terminates (which I don''t want).



如果我误解了你想要的东西,我很抱歉,但它听起来像你只需要一个封闭的尝试/除外。

d = Device.open()
尝试:
尝试:
d。 someMethodTanCanRaiseError(...)
如果SomeCondition:
引发错误#Error是Exception的子类
d.someMethodThatCanRaiseError(...)
... d上有很多其他方法这最终会引发错误:
d.close()
除了:
#无论你想要什么

这有什么不对吗?



I''m sorry if I misinterpret what you want, but it sounds like you
need nothing more than an enclosing try/except.

d = Device.open()
try:
try:
d.someMethodThatCanRaiseError(...)
if SomeCondition:
raise Error # Error is subclass of Exception
d.someMethodThatCanRaiseError(...)
... lots of other methods on d that can raise Error
finally:
d.close()
except:
# whatever you want here
Is anything wrong with that?




但他似乎并不是他想要的。我能做的最好的事情就是他需要一个处理器来接受控制

的异常,但是这个例外就足够了

导致放松到下一个处理程序。对于过程中的每个步骤,他都有一个特定的

异常处理程序,但是没有b $ b想要在处理程序中明确决定下一步的位置。 />
没有真正看到它与终于有什么关系,除了

,在他的例子中它是下一个处理程序。


Donn Cave, do**@u.washington.edu


Peter Hansen写道:
Peter Hansen wrote:
djw写道:
djw wrote:
我有一个关于正确的问题。使用try:finally:...考虑一下这样的代码:
[...]
当我运行这段代码,并且出现错误时,我得到追溯(我是
不想要)并且程序终止(我不想要)。
I have a question regarding the "proper" use of try: finally:... Consider
some code like this:
[...]
When I run this code, and an Error is raised, I get traceback (which I
don''t want) and the program terminates (which I don''t want).



如果我误解了你想要的东西我很抱歉,但它听起来像你只需要一个封闭的尝试/除外。

d = Device.open()
尝试:
尝试:
d。 someMethodTanCanRaiseError(...)
如果SomeCondition:
引发错误#Error是Exception的子类
d.someMethodThatCanRaiseError(...)
... d上有很多其他方法这最终会引发错误:
d.close()
除了:
#无论你想要什么

这有什么不对吗?



I''m sorry if I misinterpret what you want, but it sounds like you
need nothing more than an enclosing try/except.

d = Device.open()
try:
try:
d.someMethodThatCanRaiseError(...)
if SomeCondition:
raise Error # Error is subclass of Exception
d.someMethodThatCanRaiseError(...)
... lots of other methods on d that can raise Error
finally:
d.close()
except:
# whatever you want here
Is anything wrong with that?




好​​的,是的,我认为你是对的。对我而言,最明显的是:

最后:并没有阻止异常继续传播。我只是

希望你可以混合除外:最后:在同一个尝试中:阻止。


谢谢,


Don



OK, yeah, I think you are correct. What wasn''t apparent to me is that a
finally: doesn''t stop the exception from continuing to propagate. I just
wish you could intermix except: and finally: within the same try: block.

Thanks,

Don


这篇关于试试:最后:问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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