Try Catch Opinion

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
 
T

Thomas T. Veldhouse

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].
 
S

Stoitcho Goutsev \(100\)

Yes, you can return from catch block it is completely correct.
 
Z

ZenRhapsody

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.
 
G

Guest

Thanks to all for the input. The particular scenarios I want to implement
won't incorporate a finally block.

Thanks...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top