File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. unreachable code 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 "Reg. unreachable code " Watch "Reg. unreachable code " New topic

Reg. unreachable code

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
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;
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

Joined: Aug 28, 2001
Posts: 84
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.
I agree. Here's the link:
subject: Reg. unreachable code
It's not a secret anymore!