C#和Final中C#之间的区别

问题描述:

嗨...
我正在学习例外情况并对此表示怀疑.
我在try和catch中很清楚,但是不知道finally在其中的用途是什么...也无法获得catch和finally之间的区别,请举一个小例子

Hi...
Im learning about exceptions and having doubt in that.
Im clear in try and catch but dont know what is the use of finally in it...also cant get the difference between catch and finally,guide with small example

catch子句仅允许您处理代码可能引发的异常.捕获括号内的代码仅在抛出异常的情况下执行.
总括起来的代码始终执行,这使您可以清理资源,例如避免内存泄漏.
The catch clause simply allows you to handle exceptions that your code may throw. The code inside the catch braces is only executed in case an exception is thrown.
The code inside the finally braces is always executed which allows you to clean up resources in order to e.g. avoid memory leaks.
try
{
  //code that may throw exceptions
}
catch (Exception ex)
{
  //Exception handler
}
finally
{
  //Code that needs to be executed to clean up the prior code (e.g. to avoid memory leaks and to free resources)
}


一旦您不知道最终"做什么,就无法保持对尝试捕获的了解. finally块放置在try-catch-finally或try-finally构造的末尾,并在所有可能的情况下执行:是否在try块中引发了异常,是否捕获了异常,或者catch块了存在与否.就这么简单.

您想要的小例子可以在这里找到:
http://msdn.microsoft.com/en-us/library/dszsf989%28v = vs.100%29.aspx [ ^ ].

没找到自己的东西真是太好了.这样不仅可以节省我或其他专家的时间,甚至可以节省您的时间.与提出问题相比,某些事情在文档中更容易找到.

此外,我建议您注意问题的正确性:"{0}和{1}有什么区别?"形式的问题.几乎永远是不正确的;这在您的情况下是不正确的.如果您仍然没有得到它,请尝试告诉我们:apple和Apple有什么区别?

也请看看我过去的答案:编程中的类和封装之间有什么区别 [如何问一个好问题? [ ^ ].

祝你好运,
—SA
As soon as you don''t know what "finally" does, you cannot maintain that you are clear about try-catch. The finally block is placed at the end of try-catch-finally or try-finally construct and is executed in all possible cases: if exception was thrown in a try block or not, if it was caught or not, if catch blocks exist or not. As simple as that.

The small example you wanted can be found here:
http://msdn.microsoft.com/en-us/library/dszsf989%28v=vs.100%29.aspx[^].

It''s wonderful that you did not find it by yourself; it would save not only my or other expert time, but even your time. Certain things are much easier to find in documentation compared to asking questions.

Besides, I would advise you to pay attention for correctness of your questions: the question of the form "what is the difference between {0} and {1}?" is almost never correct; and it is not correct in your case. If you still did not get it, please try to tell us: what is the difference between apple and Apple?

Please also take a look at my past answer: what is the difference between the class and encapsulation in programming[^].

And please see this discussion on asking good questions:
How to ask a good question?[^].

Good luck,
—SA




请点击链接以了解有关try-catch的更多信息,最后:

http://msdn.microsoft.com/en-us/library/s7fekhdy% 28v = vs.71%29 [ ^ ]

一切顺利..
--AK
Hi,

Please follow the link to learn more about try-catch and finally:

http://msdn.microsoft.com/en-us/library/s7fekhdy%28v=vs.71%29[^]

All the best..
--AK