Try Catch Opinion

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about a
catch block?

If while in a method call you fall into a catch and don't want to continue
processing the rest of the code in the method is it ok to 'return' from the
catch?

Thanks,
John
 
John F said:
Hello All,

I know you shouldn't issue a 'return' from a finally block, but how about a
catch block?

If while in a method call you fall into a catch and don't want to continue
processing the rest of the code in the method is it ok to 'return' from the
catch?

I don't see why not, although I think it is not very clean. Even if you
"return" from a catch block, the code in the finally block will be executed,
and thus, the flow is not obviously clean. In fact, it makes much more sense
to return from a finally block then a catch block for this reason alone [I
would do neither].
 
Sure, it's ok.

Realize that if you have

try
{ yada yada yada.....}
catch
{ return;}
finally
{... some cleanup code }

The code in the finally block is executed immediately AFTER the return
statement.

I do agree with another poster that this may decrease readability of your
code. If I need some construct like this, I typically comment at the top
and bottom of the method (and probably in the try/catch area also) in a way
that i can't overlook.
 
Thanks to all for the input. The particular scenarios I want to implement
won't incorporate a finally block.

Thanks...
 
Back
Top