相关疑难解决方法(0)

如果我在Try块中返回一个值,那么finally语句中的代码会触发吗?

我正在为一位朋友审查一些代码,并说他在try-finally块中使用了一个return语句.即使try块的其余部分没有,Finally节中的代码是否仍会触发?

例:

public bool someMethod()
{
  try
  {
    return true;
    throw new Exception("test"); // doesn't seem to get executed
  }
  finally
  {
    //code in question
  }
}
Run Code Online (Sandbox Code Playgroud)

.net c# exception-handling try-catch

228
推荐指数
7
解决办法
7万
查看次数

标签 统计

.net ×1

c# ×1

exception-handling ×1

try-catch ×1