C# 返回后最终会运行吗?

C# 返回后最终会运行吗?,c#,exception-handling,return,try-catch,C#,Exception Handling,Return,Try Catch,我有一段代码: try{ this.connection.Open(); cmd.ExecuteScalar(); return true; } catch(Exception exc){ throw exc; } finally{ this.connection.Close(); } 我知道如果catch抛出异常,finally块将无论如何运行 但是try中的返回值呢 如果try块返回true,那么块是否会最终关闭我的连接 这安全吗?是的,安全。像你现

我有一段代码:

try{
    this.connection.Open();
    cmd.ExecuteScalar();
    return true;
}
catch(Exception exc){
    throw exc;
}
finally{
    this.connection.Close();
}
我知道如果
catch
抛出异常,
finally
块将无论如何运行

但是
try
中的返回值呢

如果
try
块返回
true
,那么
块是否会最终关闭我的连接


这安全吗?

是的,安全。像你现在这样工作是安全的

退出代码块后,它最终执行
,无论这是否由
返回引起。

MSDN状态

finally用于保证语句块的代码执行,而不管前面的try块是如何退出的


所以,是的。

为什么不在finally()上阅读MSDN?