C#:为什么要费心使用“finally”?条款?
可能的重复:
为什么在 C# 中使用finally?
在 C# 中,使用 finally
子句?
例如。
try {
// do something
}
catch (Exception exc)
{
// do something
}
// do something
最后的代码不会执行吗? finally
块有什么意义?
Possible Duplicate:
Why use finally in C#?
In C#, what is the point of having a finally
clause?
eg.
try {
// do something
}
catch (Exception exc)
{
// do something
}
// do something
Won't the code at the end execute anyway? What is the point of a finally
block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
finally 确保块中的任何内容都将被执行,无论前两条语句是否被激活。
一个很好的例子是释放数据库资源。
尝试-捕获-最终
示例:
The finally ensures that anything in the block will be executed, regardless if the previous two statements are activated.
A good example would be to release database resources.
Try-Catch-Finally
Example:
无论之前的代码是否成功,您都希望执行某些代码。通过使用 Try/Catch/Finally,您可以从错误处理中受益。
There are pieces of your code that you want to execute regardless of the success of previous code. By using Try/Catch/Finally you are able to benefit from the error handling.
因为您应该对某些对象执行清理,否则会给您的应用程序带来问题。
经常使用的示例是使用 SqlConnection:
在这种情况下,SqlConnection 保持打开状态,无法关闭它,因为您处理了 SqlException,但抛出了 ArgumentException。如果您使用finally块,则不会发生这种情况,因为finally块代码将执行:
Because there are certain objects that you should perform cleanup on, otherwise it will cause issues for your application.
The constantly used example is using a SqlConnection:
In this case, the SqlConnection is left open with no way of closing it because you handle a SqlException, but ArgumentException is thrown. If you used a finally block, this wouldn't happen because the finally block code would execute:
当捕获异常时,
finally
块保证运行,即使异常处理块抛出更多异常。通常用于清理某些类型的资源,例如打开的文件句柄、网络连接等...这里有很好的例子:
http: //dotnetperls.com/finally
http://www.csharp-station .com/Tutorials/lesson15.aspx
The
finally
block is garaunteed to run when an exception is caught, even if the exception handling block throws more exceptions. Often used for cleaning up certain kinds of resources such as open file handles, network connections, etc...Good examples here:
http://dotnetperls.com/finally
http://www.csharp-station.com/Tutorials/lesson15.aspx
Final 确保无论尝试成功还是失败,其中的任何内容都会被触发。在发生捕获的情况下,您不能保证最后
//do some
处的所有内容都会发生。The Finally ensures that whatever is in it will fire if the try works or fails. In a scenario where a catch occurs you are not guaranteed that everything at the last
//do something
will occur.无论是否抛出异常,Finally 块中的代码始终都会执行。如果路径在 catch 中返回,则异常块后面的代码可能会执行,也可能不会执行。
The code in the Finally block always executes, regardless of if an exception was thrown or not. The code following the exception block may or may not execute if the path returns in the catch.
finally 是针对即使 catch 抛出异常的情况,加上它允许您在成功和失败时执行可爱的代码,finally 总是会被执行。总是。
好的,除非应用程序在系统级别被终止或计算机爆炸。
Finally is for the event that even the catch throws an exception, plus it allows you to exewcute code on success and failure, the finally will ALWAYS be executed. ALWAYS.
Ok, except when the application is killed at the system level or the computer explodes.