I had the following two lines of code:
RegisteredForMeasurement = false;
return EngineCommands.CommandReset();
Now I need to change this so RegisteredForMeasurement
is set to false
AFTER the CommandReset
. I have two possiblities I can think of:
var result = EngineCommands.CommandReset();
RegisteredForMeasurement = false;
return result;
or
try { return EngineCommands.CommandReset(); }
finally { RegisteredForMeasurement = false; }
Aside from the fact, that in version 2 RegisteredForMeasurement
is also set to false
if CommandReset
throws an exeption, what would you prefer. Or is there an even better or more elegant way?
Is version 2 faster, as it does not need a temporary local variable or does it end up to be about the same after JIT-optimizations?
People always say you should not use try
/catch
for "program logic" - is this also true for try
/finally
?
I used the second version a few times in the past, when I had multiple return
s and needed to execute something after all of them, as it "reads more natural" to me ("do FINALLY after everything else is done").