Will Code in a Finally Statement Fire If I Return a Value in a Try Block

Will code in a Finally statement fire if I return a value in a Try block?

Simple answer: Yes.

C# - Try-Catch-Finally on Return

Yes.

As stated here: MSDN

Typically, the statements of a finally block run when control leaves a
try statement. The transfer of control can occur as a result of normal
execution, of execution of a break, continue, goto, or return
statement, or of propagation of an exception out of the try statement.

But finally block is not always executed. You can read Alex Papadimoulis's anecdote here

Will finally blocks be executed if returning from try or catch blocks in C#? If so, before returning or after?

Yes, the finally block is executed however the flow leaves the try block - whether by reaching the end, returning, or throwing an exception.

From the C# 4 spec, section 8.10:

The statements of a finally block are always executed when control leaves a try
statement. This is true whether the control transfer occurs as a result of normal
execution, as a result of executing a break, continue, goto, or return statement, or as a
result of propagating an exception out of the try statement.

(Section 8.10 has a lot more detail on this, of course.)

Note that the return value is determined before the finally block is executed though, so if you did this:

int Test()
{
int result = 4;
try
{
return result;
}
finally
{
// Attempt to subvert the result
result = 1;
}
}

... the value 4 will still be returned, not 1 - the assignment in the finally block will have no effect.

Is placing return statement before finally block a good practice?

Putting return in the try or even in the catches is absolutely fine: do whatever is clearest. Take care not to discard too many exceptions though, particularly java.lang.Throwable as doing that can interfere with the workings of the JVM.

Note that if you have a return in the finally block, then the expression in the other return is still evaluated but the result is discarded and the return value in the finally block is returned back to the caller. Because of this, putting a return value in the finally block is discouraged.

return in try-catch's finally block in java. Is there any good point in this example?


Is there really any point in adding a finally block here?

The answer to this is a resounding "no": putting a return statement in the finally block is a very bad idea.

I just add the return null inside the catch block, which would execute the same behavior, or am I wrong?

It wouldn't match the original behavior, but that's a good thing, because it would fix it. Rather than returning null unconditionally the way the original code does, the code with the return inside the catch block would return null only on errors. In other words, the value returned in the try branch would be returned to the caller unless there is an exception.

Moreover, if you add return null after the catch block, you would see the correct effect of returning null on exception. I would go even further, and put a single return in the method, like this:

response or = null;
try {
or = //gives it a value
log.info(or.toString());
} catch ( Exception e) {
e.printStackTrace();
}
return or;

Will finally run after return?

Yes, it does. It is safe to work as you do now.

It executes the finally after you exit the code block, no matter if that is caused by a return or not.

why is my return statement unreachable after try-finally?

You have no catch, so if there's an exception in your try it will just be thrown. Your method either succeeds and returns true, or an exception is thrown.

If you add a catch block, the warning will go away because that path is now accessible.

You should be able to swallow the exception (if that's what you want) by simply adding catch {} before the finally.

Looking at your code more closely, you likely want to replace the finally with a catch anyways- it's unlikely you wish to log that the delete had failed no matter the outcome.

Is it bad practice to return from within a try catch finally block?

No, it's not a bad practice. Putting return where it makes sense improves readability and maintainability and makes your code simpler to understand. You shouldn't care as finally block will get executed if a return statement is encountered.



Related Topics



Leave a reply



Submit