为什么要使用“最后” [英] Why use "finally"

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

问题描述




这可能是一个简单的问题,但我很好奇。

的目的是什么?终于声明?必须有充分的理由使用它我只是

没有得到...


这是怎么回事:


方法1

{

试试

{

x;

}

catch

{

y;

}

终于

{

z;

}

}


不同于:


方法1

{

尝试

{

x;

}

catch

{

y;

}


z;

}


谢谢,

Joe

Hi,

This is probably a simple question but I''m curious. What is the purpose of
the "finally" statement? There must be a good reason to use it that I''m just
not getting...

How is this:

method1
{
try
{
x;
}
catch
{
y;
}
finally
{
z;
}
}

different than:

method1
{
try
{
x;
}
catch
{
y;
}

z;
}

Thank you,
Joe

推荐答案

通常,在Try或Catch中都会有return语句,或者

。把这个代码放在最后保证它将被执行

无论如何。除非另有说明,否则你必须在返回之前将它的副本放在

Try and Catch中。


" Joe Thompson" <乔********* @ discussions.microsoft.com>在留言中写道

新闻:6D ********************************** @ microsof t.com ...
Often, there will be return statements either in the Try or the Catch, or
both. Putting this code in the finally guarantees that it will be executed
in any case. Where as otherwise, you would have to put a copy of it in the
Try and Catch right before the return.

"Joe Thompson" <Jo*********@discussions.microsoft.com> wrote in message
news:6D**********************************@microsof t.com...


这可能是一个简单的问题,但我很好奇。什么是终于的目的是什么?声明?必须有充分的理由使用它,我只是没有得到......

这是怎么回事:

method1 <
{
x
}
{/> 最后
{
z;
}
}

不同于:

方法1
{
尝试
{
x;
}
抓住
{
y;
}

z;
}

谢谢你,
Joe
Hi,

This is probably a simple question but I''m curious. What is the purpose
of
the "finally" statement? There must be a good reason to use it that I''m
just
not getting...

How is this:

method1
{
try
{
x;
}
catch
{
y;
}
finally
{
z;
}
}

different than:

method1
{
try
{
x;
}
catch
{
y;
}

z;
}

Thank you,
Joe



释放你在try块中可能拥有的对象。 MSDN上的以下

文章可能最能解释它:

http://msdn.microsoft.com/library/de...ogether_PG.asp


-

Manohar Kamath

编辑,.netWire
www.dotnetwire.com

" Joe Thompson" <乔********* @ discussions.microsoft.com>在留言中写道

新闻:6D ********************************** @ microsof t.com ...
To release objects that you might have in the try block. The following
article on MSDN probably best explains it:

http://msdn.microsoft.com/library/de...ogether_PG.asp

--
Manohar Kamath
Editor, .netWire
www.dotnetwire.com
"Joe Thompson" <Jo*********@discussions.microsoft.com> wrote in message
news:6D**********************************@microsof t.com...


这可能是一个简单的问题,但我很好奇。目前
的目的是什么?终于声明?必须有充分的理由使用它,我只是没有得到... b / b
这是怎么回事:

方法1
{
尝试
{
x;
}
抓住
{
y;
}
终于 {
z;
}
}

不同于:

方法1
{
尝试
{
x;
}
抓住
{
y;
}

z;
}

谢谢,
Joe
Hi,

This is probably a simple question but I''m curious. What is the purpose of the "finally" statement? There must be a good reason to use it that I''m just not getting...

How is this:

method1
{
try
{
x;
}
catch
{
y;
}
finally
{
z;
}
}

different than:

method1
{
try
{
x;
}
catch
{
y;
}

z;
}

Thank you,
Joe



答案是在你的第二个例子中,z和并不保证执行

,而在你的第一个例子中,它是。


但有一点需要注意。你的具体例子有点不切实际:仅仅有一个捕获的东西就是坏的风格。不说要抓什么,一个

" catch-all"如果你原谅双关语。所以你的例子写的方式是b,这是z的唯一方法。将不会在第二个中执行

示例是否为y抛出异常(在catch块内)。


更现实的情况是捕获的情况。阻止

特定例外情况,在这种情况下你需要最终如果出现其他一些你没有捕捉到的b $ b异常的情况。


有时候人们(比如我)甚至会使用终于。没有捕获在所有:

我们希望异常渗透到调用者,但我们必须确保释放一些资源,重置某些值或更改某些设置离开前



在我的代码中,将光标更改为沙漏就是一个很好的例子:


/ /将光标更改为沙漏

尝试

{

....做一个漫长,缓慢,复杂的操作...

}

终于

{

//将光标更改回默认值

}


我将光标更改回finally块中的默认值因为我想要发生这种情况_even if_try中的复杂操作

爆炸了。事实上,操作非常复杂,因为我无法控制的原因已经多次炸毁了我。如果我的

将光标改回代码不在最后范围内然后我的

光标会被卡在沙漏中。

The answer is that in your second example, "z" is not guaranteed to be
executed, whereas in your first example, it is.

One caveat, though. Your specific examples were a bit unrealistic: it''s
bad style to simply have a "catch" without saying what to catch, a
"catch-all" if you''ll pardon the pun. So the way that your examples are
written, the only way that "z" will not be executed in the second
example is if "y" throws an exception (inside the catch block).

A more realistic case would be one in which the "catch" blocks catch
specific exceptions, in which case you need "finaly" in case some other
kind of exception that you''re not catching occurs.

Sometimes people (like me) even use "finally" with no "catch" at all:
we want the exceptions to percolate up to the caller, but we have to be
sure to release some resource, reset some value, or change some setting
before leaving.

In my code, changing the cursor to an hourglass is a good example:

// Change the cursor to an hourglass
try
{
.... do a long, slow, complicated operation...
}
finally
{
// Change the cursor back to the default
}

I change the cursor back to a default in the finally block because I
want that to happen _even if_ the complex operation inside the "try"
blows up. In fact, the operation is complex enough that it has already
blown up on me several times for reasons beyond my control. If my
"Change the cursor back" code weren''t within the "finally" then my
cursor would be stuck as an hourglass.


这篇关于为什么要使用“最后”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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