c# - 尝试使用 C sharp 中的 catch block

标签 c# asp.net

我在我的项目中使用了 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/

相关文章:

c# - 如何将 ASP.NET 隐藏字段值分配给 JavaScript 变量?

javascript - GraphQL 和 dataLoader 在不同的应用程序/服务器上

支持 JSONP 的 ASP.NET 通用 HTTP 处理程序 (.ashx)

c# - 将各种数据一起发布的最佳方式(Web API)

asp.net - 在现有应用程序中集成 ASP.Net 和 ('classic' ) ASP

c# - 如何配置 ASP.NET 应用程序的环境?

c# - 如何为具有特定属性的对象创建 Linq 扩展?

asp.net - 如何测试支持多个域的 Multi-Tenancy 应用程序

c# - 无法在 Entity Framework 中使用 SQL 查询获取数据

c# - 如何在按键持续一定时间后重复操作?