File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes where to use return statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "where to use return statement" Watch "where to use return statement" New topic
Author

where to use return statement

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844

what is the best practice, should i use return statement inside try ?



or
what is the best practice, should i use return statement after catch ?


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Can you do that? If an exception arise, and your return type isn't void, what will it return? I think that's a compile time error.
Saravanan Thoppae
Greenhorn

Joined: Jun 30, 2007
Posts: 13
This code will compile as any line after the return statement will be unreachable. When exception is raised, finally block executes and then the exception is propagated.

Regarding where to use return statement, i would say it is preferable to use it at the end of the method so that any person who look the code has to look in only at one place. But some people argue that why should the code process other lines of code when it knows it has done its job at that time.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
thanks for reply.. Any other logical explanation.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You can't outsmart the compiler, and if you try to get away with possibly letting your method complete normally without returning a String, it will get upset. If you want to put a return only in your try block, you must also put a return in all your catch blocks (or, alternatively, a single return in your finally block.) Only then the compiler can make sure that if your method completes normally it will return what it promised in its signature.

Look at this example:



If you have a return after the try/catch/finally structure (// 4), it will always compile. If you don't, then you have two options:
1. Have a return in your finally block (// 3) or
2. Have a return in your try block and in all your catch blocks (// 2).

This is true even if you put the return statement as the first statement in your try block.

What's interesting is that the compiler doesn't care whether you have a return in your try block, as long as you have a return in your finally block. That makes sense, because in that case a return in the try block would be superfluous as it would never be executed, since a return in the finally block would always be executed first.

So to answer your question, it depends on what you want to do. But I think the best thing to do would be to return outside the try/catch/finally structure. If you can reach the finally block with an outstanding exception, then handle the exception at the point of the call (because in that case your method will not complete normally and will return nothing.)

I hope this helps.


All code in my posts, unless a source is explicitly mentioned, is my own.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
thanks Ruben for your reply.
if you try to execute my code above and throwing exception inside it.

it works both way. don't consider finally block in my example.

if i use return inside the try, i expected on throwing exception it will return. but compiler is clever, it will not look
for return in between the execution path. it will directly go to catch clause without looking what are the statement's
after the line from where exception is thrown.

compiler is fine- if you have return inside try or after catch.

to me it looks. it's a matter of interest whichever you choose.

but i would prefer use return after catch.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Amandeep Singh wrote:thanks Ruben for your reply.
if you try to execute my code above and throwing exception inside it.

it works both way. don't consider finally block in my example.

if i use return inside the try, i expected on throwing exception it will return. but compiler is clever, it will not look
for return in between the execution path. it will directly go to catch clause without looking what are the statement's
after the line from where exception is thrown.

compiler is fine- if you have return inside try or after catch.

to me it looks. it's a matter of interest whichever you choose.

but i would prefer use return after catch.

Hi Amandeep,

It sounds like your doubts are cleared. Your example is a particular case of my more general example (which includes the finally block.) If you have a return inside the try you also need a return inside the catch in order for the code to compile. Otherwise, you could throw an exception inside the try, catch it, and not return anything (with the method completing normally.) These kinds of things the compiler will prevent you from doing.

I didn't discuss unreachable statements, but if you for example have a return in your finally block, then you can't have any code after the try/catch/finally, because the return statement inside a finally block is always executed.

Regards,

Ruben
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: where to use return statement
 
Similar Threads
silly but severe problem
Reg. return from try-catch-finally
try, catch,finally
behaviour of finally
Best Practice where to write return statement?