'终于'的目的? [英] purpose of 'finally' ?

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

问题描述




关于异常处理:

为什么把代码放在''finally''块中,而不仅仅是''catch'之后'-block - >


使用''finally''的示例:


尝试

{

OpenFile();

}

catch(Exception exc)

{...}
终于

{

//代码放在这里以确保文件将被关闭

//无论我们是否有没有例外

CloseFile();

}


示例不使用''finally'':

试试

{

OpenFile();

}

catch(Exception exc)< br $> b $ b {...}

CloseFile();


我们有同样的效果,不是吗?

为什么要使用''finally''?


Thnx


Chris

Hi,

regarding exception-handling :
why put code in a ''finally'' block, and not just after a ''catch''-block -->

Example using ''finally '' :

try
{
OpenFile();
}
catch (Exception exc)
{ ... }
finally
{
// Code put here to make sure that file will be closed
// regardless of whether we have a exception or not
CloseFile();
}

Example NOT using ''finally '' :
try
{
OpenFile();
}
catch (Exception exc)
{ ... }
CloseFile();

We have the same effect, no ?
Why use ''finally'' at all ?

Thnx

Chris

推荐答案

保证finally块中的任何代码都被执行在

例程退出之前。例如,如果您的代码的另一部分抛出了您不期望的

异常,该怎么办?如果catch块没有
包括被抛出的特殊异常怎么办?在这两个

的情况下,finally块总是被执行。所以最终

块的目的是为了整理,处理等例程中使用的任何变量

才退出。


" Chris" < CH ******** @ pandora.be>在消息中写道

新闻:fZ ******************** @ phobos.telenet-ops.be ...
Any code in the finally block is guaranteed to be executed before the
routine exits. For example, what if another part of your code throws an
exception that you weren''t expecting? What if the catch block doesn''t
include the particular exception that is being thrown? In both of these
cases the finally block is always executed. So the purpose of the finally
block is really to tidy up, dispose etc. any variables used in the routine
before it exits.

"Chris" <ch********@pandora.be> wrote in message
news:fZ********************@phobos.telenet-ops.be...


关于异常处理:
为什么将代码放在''finally''块中,而不是仅仅在
''catch''之后 - 阻止 - >
使用''finally''的例子:

尝试
{
OpenFile();
}
catch(异常) exc)
{...}
最后
{
//代码放在这里以确保文件将被关闭
//无论我们是否有是否异常
CloseFile();
}

示例不使用''finally'':
尝试
{
OpenFile() ;
}
catch(Exception exc)
{...}
CloseFile();

我们有同样的效果,没有?
为什么要使用''终于''?

Thnx

Chris
Hi,

regarding exception-handling :
why put code in a ''finally'' block, and not just after a ''catch''-block -->
Example using ''finally '' :

try
{
OpenFile();
}
catch (Exception exc)
{ ... }
finally
{
// Code put here to make sure that file will be closed
// regardless of whether we have a exception or not
CloseFile();
}

Example NOT using ''finally '' :
try
{
OpenFile();
}
catch (Exception exc)
{ ... }
CloseFile();

We have the same effect, no ?
Why use ''finally'' at all ?

Thnx

Chris





" ;克里斯" < CH ******** @ pandora.be>在消息中写道

新闻:fZ ******************** @ phobos.telenet-ops.be ...

"Chris" <ch********@pandora.be> wrote in message
news:fZ********************@phobos.telenet-ops.be...


关于异常处理:
为什么将代码放在''finally''块中,而不是仅仅在
''catch''之后 - 阻止 - >
使用''finally''的例子:

尝试
{
OpenFile();
}
catch(异常) exc)
{...}
最后
{
//代码放在这里以确保文件将被关闭
//无论我们是否有是否异常
CloseFile();
}

示例不使用''finally'':
尝试
{
OpenFile() ;
}
catch(Exception exc)
{...}
CloseFile();

我们有同样的效果,没有?
为什么要使用''finally''?
Hi,

regarding exception-handling :
why put code in a ''finally'' block, and not just after a ''catch''-block -->
Example using ''finally '' :

try
{
OpenFile();
}
catch (Exception exc)
{ ... }
finally
{
// Code put here to make sure that file will be closed
// regardless of whether we have a exception or not
CloseFile();
}

Example NOT using ''finally '' :
try
{
OpenFile();
}
catch (Exception exc)
{ ... }
CloseFile();

We have the same effect, no ?
Why use ''finally'' at all ?



在你的第二个例子中你吃掉了异常。非常糟糕。


如果您在CloseFile()之后有代码,它将继续执行,您将不知道发生了异常。


而且你还需要在try块之前打开资源,因为如果它打开了
就失败了,你就不应该试图关闭它。


最后不是绝对必要的,但是等效的构造看起来像


OpenFile();

尝试

{

UseFile();

}

catch(Exception exc)

{

CloseFile();

throw;

}


David


In your second example you have eaten the exception. Very bad.

If you had code after CloseFile() it would continue to execute and you would
not know that an exception occured.

And also you need to open the resource before the try block, since if it
fails in opening, you shouldn''t try to close it.

Finally is not strictly necessary, but the equivilent constuct looks like

OpenFile();
try
{
UseFile();
}
catch (Exception exc)
{
CloseFile();
throw;
}

David





理由是如果一个例外被抛出但没有被抓住,你就不会这么做了b $ b关闭你的文件。例如,你的try块可能会调用抛出了catch子句未涵盖的异常的代码 - 在这种情况下你会变得非常糟糕

问题。有些语言没有最终的子句(例如本机C ++

),另外还有一种替代方法可以封装潜在的狡猾的
代码,这样它就可以保证当它超出

范围时自行清理,但通常最终'确保可靠代码是一种很好的方式

当它是两条线之一时这可能会导致问题。


史蒂夫


" Chris" < CH ******** @ pandora.be>在消息中写道

新闻:fZ ******************** @ phobos.telenet-ops.be ...
Hi,

The reasoning is that if an exception''s thrown but not caught, you wouldn''t
close your file. For example, your try block might call code that threw
exceptions not covered by the catch clause - in this case you''d get terrible
problems up the wazoo. Some languages don''t have finally clauses (native C++
for example), and the alternative there is to encapsulate potentially shifty
code so that it''s guaranteed to clean up after itself when it goes out of
scope, but generally finally''s quite a nice way of ensuring reliable code
when it''s one of two lines that could cause problems.

Steve

"Chris" <ch********@pandora.be> wrote in message
news:fZ********************@phobos.telenet-ops.be...


关于异常处理:
为什么将代码放在''finally''块中,而不是仅仅在
''catch''之后 - 阻止 - >
使用''finally''的例子:

尝试
{
OpenFile();
}
catch(异常) exc)
{...}
最后
{
//代码放在这里以确保文件将被关闭
//无论我们是否有是否异常
CloseFile();
}

示例不使用''finally'':
尝试
{
OpenFile() ;
}
catch(Exception exc)
{...}
CloseFile();

我们有同样的效果,没有?
为什么要使用''终于''?

Thnx

Chris
Hi,

regarding exception-handling :
why put code in a ''finally'' block, and not just after a ''catch''-block -->
Example using ''finally '' :

try
{
OpenFile();
}
catch (Exception exc)
{ ... }
finally
{
// Code put here to make sure that file will be closed
// regardless of whether we have a exception or not
CloseFile();
}

Example NOT using ''finally '' :
try
{
OpenFile();
}
catch (Exception exc)
{ ... }
CloseFile();

We have the same effect, no ?
Why use ''finally'' at all ?

Thnx

Chris



这篇关于'终于'的目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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