File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where to use return statement

 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ?
 
Ryan Beckett
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for reply.. Any other logical explanation.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1032
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic