终于 { //代码放在这里以确保文件将被关闭 //无论我们是否有没有例外 CloseFile(); } 示例不使用''finally'': 试试 { OpenFile(); } catch(Exception exc) 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 notCloseFile();} 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 theroutine exits. For example, what if another part of your code throws anexception that you weren''t expecting? What if the catch block doesn''tinclude the particular exception that is being thrown? In both of thesecases the finally block is always executed. So the purpose of the finallyblock is really to tidy up, dispose etc. any variables used in the routinebefore it exits. "Chris" <ch********@pandora.be> wrote in messagenews: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 messagenews: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 wouldnot know that an exception occured. And also you need to open the resource before the try block, since if itfails 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''tclose your file. For example, your try block might call code that threwexceptions not covered by the catch clause - in this case you''d get terribleproblems up the wazoo. Some languages don''t have finally clauses (native C++for example), and the alternative there is to encapsulate potentially shiftycode so that it''s guaranteed to clean up after itself when it goes out ofscope, but generally finally''s quite a nice way of ensuring reliable codewhen it''s one of two lines that could cause problems. Steve "Chris" <ch********@pandora.be> wrote in messagenews: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 这篇关于'终于'的目的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 02:57