Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Return in a try block  RSS feed

 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does the following code fail... i am returning a string object.. is this because there is a possibillity of an error which would proceed to the catch block ...but isnt this a runtime issue.. why is this at compile time check??

public String acb()
{
try{

return "sdf";
}
catch(Exception e)
{
e.printStackTrace();
}
}
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I expect your compile error to say something along the lines of "acb must return a String".

If an exception were to be caught in the catch block then all you do is print the stack trace. It would then proceed to run the next line of code after the try/catch. In this case you don't do anything.
The compiler is intelligent enough to realize that its possible for the logic flow to not return a String.
 
Shakeel Shaik
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should compile just fine.
What is the error that you are getting?
You haven't mentioned the error details for anybody to solve your problem.
 
Shakeel Shaik
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paul's right. I missed that part.
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
acb must return a String... this is the compiler complaint
 
Stanley Walker
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but there is somethign i dont understand...

if i do the following
public String acb()
{
try{

return "sdf";
}
catch(Exception e)
{
e.printStackTrace();
}
return "as";
}
the compiler is happy that my method is returning a string as declared no matter what.
how about if i change the method to
public String acb() throws Exception
{
//
//
//
return "sdf";
}
it is very possible that even before the compiler encounters the return statement it may run into an exception ...
so the above would compile because the calling method has to either catch the thrown exception or throw it itself... is this why it doesnt complain.. or am i getting this wrong.
i am sorry guys , i know what i am asking seems pretty juvenile.. but somehow i got confused.
 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stanley

This code compiles perfectly because your method except some string to be returned no matter what. In case an excpetion is caught return "sdf" would not be executed and method expect some string to be returned. But when you write return after try block it means if exception is caught then after try it has something to be returned for this method.

Try one more thing - add return statement in catch in the above code( means you are having 3 returns now), this code will not be compile as the last return - return "as" in your case is a statement that will never be executed.

Regards
Patricia.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its also generally considered good practice to only have one return in a method.

eg:



 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Beckett wrote:its also generally considered good practice to only have one return in a method.

Many people agree with this, and many do not. I don't think it's at all correct to assert that it's generally considered good practice - there are too many practitioners who don't agree.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO the days of "single return points" went out when C++ jumped the shark.

It only makes sense if when it's perfectly clear: twisting a method around to force a single return point isn't always the clearest way.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!