Also when I pasted this code into Eclipse it warns me that "finally block does not complete normally".
Nice question however

Moderator: admin
Code: Select all
switch( 3 ){
default :
}
switch( 4 ){
case 5 :
}
switch( 3 ){
default : ;
}
switch( 3 ){
default : {}
}
Code: Select all
try{
return;
}catch(Exception e){
return;
}catch(RuntimeException e){ //this catch block is unreachable
return;
}
finally{
if(true ) return ;
else return;
return; //this is unreachable
}
I am with you. Thought of the same question, also opted for unreachable code and failed. Will supply the question if I find it again.Solid15 wrote: ↑Sun May 05, 2019 9:04 amYesterday I had a mock question saying it doesn't compile of you return in both the try, catch and final block. (I think it was in mock exam 3, otherwise it was in mock exam 5). I believe it had to do with unreachable code. I answered this question wrong because I thought it would return the finally return. Now I see this question and I answer "does not compile" and it is wrong, because apparently the finally return can overwrite the previous return.
I'm confused. Doesn't the compiler know the finally return is unreachable because of the if-else clause within the try-block, as opposed to the previous example (if I remember the details of the previous question correctly).
Some testing in my IDE indicates that it compiles. So either the question from the previous mock test was wrong, or something else is involved there that I don't understand.
QUESTION: is there a way to make try{return}catch{return}finally{return} not compile? If so, how?
Users browsing this forum: Bing [Bot], Google [Bot] and 46 guests