Forums Register Login

Reg. unreachable code

+Pie Number of slices to send: Send
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 ?
+Pie Number of slices to send: Send
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.
Oh the stink of it! Smell my tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 866 times.
Similar Threads
exception doubt
finally block executes right after the return statement?
try-catch-finally
unreachable statements
how the compiler decides about the unreachable stmt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:05:59.