This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg. unreachable code

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is regarding unreachable code w.r.t to the return statement :

public int checkReturn()
{
int x = 0 ;
try {
System.out.println(" TRY ");
int y = x/0;
return x;
}
catch( Exception e )
{
System.out.println(" CATCH ");
return x;
}
finally
{
System.out.println(" FINALLY ");
return x;
}
// return x;
}


Point 1 :
With reference to try-catch-finally, i can have return in each
block. But i cannot un-comment the last line as finally block in
any case will be executed and so the last line becomes unreachable.
Point 2 :
We can remove the comment from the last line,but we need to comment return x of finally block.

So if the finally block is containing return, then
we don't need to declare a return after it or at the end ?
What other case of some statement becoming unreachable are
possible with reference to try-catch-finally ?
 
marilyn murphy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to return x in the finally block, it seems that the return x statements in the try and the catch blocks are unnecessary as well as the unreachable return x statement after the finally block.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic