我在我的项目中使用了 try catch 和 finally block 。我的疑问是…… 为什么我们需要使用 finally block ,实际上如果不使用 finally block 那么代码也会在 catch block 之后执行。所以我们可以在 catch block 之后做代码(做资源免费)。 即使发生异常,这也会执行。那么如果我们使用finally block 有什么好处吗?
最佳答案
根据this的回答以前的 SO 帖子:
The code inside a finally block will get executed regardless of whether or not there is an exception. This comes in very handy when it comes to certain housekeeping functions you need to always run like closing connections.
关于c# - 尝试使用 C sharp 中的 catch block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6544395/