我们已经看到很多关于何时以及为何使用try/ catch和try/ catch/的问题finally.我知道try/ 肯定有一个用例finally(特别是因为它是using语句实现的方式).
我们还看到了有关try/catch和异常开销的问题.
然而,我所链接的问题并没有谈到JUST try-finally的开销.
假设try块中发生的任何事情都没有异常,那么确保finally语句在离开try块时执行的开销是多少(有时是从函数返回)?
再一次,我只询问try/ finally,不catch,不抛出异常.
谢谢!
编辑:好的,我将尝试更好地展示我的用例.
我应该使用哪个,DoWithTryFinally或者DoWithoutTryFinally?
public bool DoWithTryFinally()
{
this.IsBusy = true;
try
{
if (DoLongCheckThatWillNotThrowException())
{
this.DebugLogSuccess();
return true;
}
else
{
this.ErrorLogFailure();
return false;
}
}
finally
{
this.IsBusy = false;
}
}
public bool DoWithoutTryFinally()
{
this.IsBusy …Run Code Online (Sandbox Code Playgroud)