相关疑难解决方法(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万
查看次数

试着抓住最后的问题

在Try Catch Finally块中,finally块总是执行,无论是什么,或者只有catch块没有返回错误?

我的印象是,如果catch块没有错误,则finally块只会执行.如果由于错误而执行了catch块,那么它是否应该一起停止执行并返回错误消息(如果有的话)?

c# exception-handling

17
推荐指数
3
解决办法
1万
查看次数

标签 统计

c# ×2

exception-handling ×2

.net ×1

try-catch ×1