About Question enthuware.ocajp.i.v7.2.1148 :
Moderator: admin
-
- Posts: 8
- Joined: Sat Aug 27, 2016 2:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
Java is weird... I also typed for unreachable block - it wont compile...
Also when I pasted this code into Eclipse it warns me that "finally block does not complete normally".
Nice question however
Also when I pasted this code into Eclipse it warns me that "finally block does not complete normally".
Nice question however
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
Suppose that seed=11 so I will get 1 instead of 7 which is returned in finally because finally is not executed compulsorily unless and until we execute the code in try block or if there is a System.exit(),Am I Right??
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
finally isn't executed if there is a System.exit is executed in the try block or catch block otherwise. Except that, a finally block is always executed.
If you like our products and services, please help us by posting your review here.
-
- Posts: 35
- Joined: Sat Nov 25, 2017 4:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
Okay thank you Paul.
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
Funny I had thought that having just "default:" would be a syntax error but clearly it isn't. All the following are legal:
Code: Select all
switch( 3 ){
default :
}
switch( 4 ){
case 5 :
}
switch( 3 ){
default : ;
}
switch( 3 ){
default : {}
}
-
- Posts: 1
- Joined: Sun May 05, 2019 8:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
Yesterday 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?
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?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
I couldn't find any explanation text that says anything like, "it doesn't compile of you return in both the try, catch and final block.".
finally block is always executed, so, a return statement in finally cannot be unreachable just because it is present in the finally block or because there is a return statement or an if/else statement in the try or catch blocks.
The rules of unreachable code are same for all types of blocks. So, a simple code like this:
will not compile.
If you can find the question that you think has a problem, let us know.
finally block is always executed, so, a return statement in finally cannot be unreachable just because it is present in the finally block or because there is a return statement or an if/else statement in the try or catch blocks.
The rules of unreachable code are same for all types of blocks. So, a simple code like this:
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
}
If you can find the question that you think has a problem, let us know.
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
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?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
You might want to check out questions 2.984, 2.1200, and 2.1323. They touch upon reachable statement issue but are different.
(In ETS Viewer s/w, you can do Control + Q and then enter 2.984 to open that question.)
(In ETS Viewer s/w, you can do Control + Q and then enter 2.984 to open that question.)
If you like our products and services, please help us by posting your review here.
-
- Posts: 38
- Joined: Thu Jan 07, 2021 2:47 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1148 :
Great. Supplying those numbers was super-helpful. Thanks admin.
The question I had in mind was 2.1200. I looked at it again and found the difference. In that question there is another return statement after the try/catc/finally block and that causes all the issues. So its not really comparable to this situation (but seemed so from my memory).
Thanks for being such a great support here...really good exam prep experience
The question I had in mind was 2.1200. I looked at it again and found the difference. In that question there is another return statement after the try/catc/finally block and that causes all the issues. So its not really comparable to this situation (but seemed so from my memory).
Thanks for being such a great support here...really good exam prep experience
Who is online
Users browsing this forum: No registered users and 52 guests