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

The finally will be executed no matter what, so it doesn't matter.


This may answer your question

What really happens in a try { return x; } finally { x = null; } statement?

From reading that question it sounds like you can have another try catch structure in the finally statement if you think it might throw an exception. The compiler will figure out when to return the value.

That said, it might be better to restructure your code anyway just so it doesn't confuse you later on or someone else who may be unaware of this as well.


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.


Personally, I would avoid this kind of coding as I don't feel like seeing return statements before finally statements.

My mind is simple and it process things rather linearly. Therefore when I walk through the code for dry running, I will have tendency to think that once I can reach the return statement, everything follow doesn't matter which obviously is pretty wrong in this case (not that it would affect the return statement but what the side effects could be).

Thus, I would arrange the code so that the return statement always appear after the finally statements.