About Question enthuware.ocajp.i.v8.2.1254 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
berensn
Posts: 4
Joined: Sat Jun 13, 2015 3:08 pm
Contact:

About Question enthuware.ocajp.i.v8.2.1254 :

Post by berensn »

In q34 (id 1148) the explanation said that since the try/catch block had a finally it was executed before the catch block

In q70 the finally isn't executed before the catch block. Why is this?

Online
admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by admin »

No, 2.1148 it doesn't say that finally is executed before catch. There is a return statement in catch and it says finally is executed before anything is returned.
If you like our products and services, please help us by posting your review here.

Walter
Posts: 5
Joined: Tue Apr 04, 2017 3:35 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by Walter »

Concerning Q69 Test 6, ID 7.2.1254

I am trying to figure out the return statement(return; no value) in general but especially pertaining to this question.
I am able to follow the flow of control, my difficulity is the return; statement and its meaning as applied in this code, which is probably nearly the same in most of its usage.

So, with the return; statement in the catch clause program prints 13423
Then commenting out the return; statement in the catch clause, program prints 134234
So to me it seems that the 'natural behaviour' of the catch clause is changed by including the return; statement - seems to have similar effect as a break statement does. Is this a fair assumption?

Then including a return; statement in the finally block causes the 'last' line of code j += "4"; to be unreachable.

Again by including the return; statement in the finally block and removing the line j += "4";, the IDE complains that the "finally block does not complete normally", once again making it seem the return; statement is changing the behaviour of the finally block, although the code does complete as expected and outputs 1323.

Some comments in Stackoverflow state not to use the return; statement (no value returned) but it does have a definite affect on the catch/finally blocks.
Any insight would be great.

Warm Regards
Walter

Online
admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by admin »

First, you need to remember that in a try/catch/finally block, the code in finally will always be executed, irrespective of whether there is or is not a return statement in the try block or the catch block.

Second, when you execute a return statement (whether return with a value or just an empty return; in case of a method that returns void), any code that appears after the return statement (except the finally block, if there is any) will not be executed.

So now, based on the above two points, you can easily deduce that if you include a return statement in finally, any code appearing after the finally block will not be executed in any case. There is no way that code can execute. Hence, the compiler complains that it is unreachable.

If you have a return statement inside a catch block, there is a possibility that the code after finally will be executed (if there is no exception thrown in the try block and therefore the catch block itself will not be executed). The compiler realizes this possibility and so it is ok.

Similarly, if you have a return statement in a try block, there is a possibility that the code after finally will be executed (if there is an exception thrown in the try block before reaching the return statement). The compiler realizes this possibility and so it is ok as well.

What is the "natural behavior" of a catch statement according to you? I need to understand that before I can address your confusion. Yes, the behavior of return is similar to break in the sense that any code after the return will not be executed (except finally, if any).
Again by including the return; statement in the finally block and removing the line j += "4";, the IDE complains that the "finally block does not complete normally", once again making it seem the return; statement is changing the behaviour of the finally block, although the code does complete as expected and outputs 1323.
Don't use any IDE while studying for the exam. Only trust the messages generated when you compile and run a program directly from command line using Oracle's JDK.

Returns statement doesn't change the behavior of anything. Just remember the two points that I mentioned above.
If you like our products and services, please help us by posting your review here.

Walter
Posts: 5
Joined: Tue Apr 04, 2017 3:35 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by Walter »

Firstly, thanks Paul for the reply much appreciated and your comments have enlightened me even if it doesn't seem so from my upcoming long windedness for which I apologise.

About the using the IDE, I mostly use Notepad++ and the command line which I have learned a lot from, which i've seen you express a number of times in the forum, I just thought the IDE might enlighten me some on this.

Regarding 'natural behaviour' I suppose I meant completing out the block, as in executing the closing curley brace regardless of whether there is code after the return;

My difficulity was(I hope) is where the return; statement takes the flow of control to or/and what it means to the compiler. It is really only from reading your comments that I have taken the compiler into consideration in earnest. I suppose I don't know enough about its workings/influence on the code and haven't taken it seriously.

Scenario is having a return; statement in try block but not catch block:
When the code exits the try block with no exception occuring, flow goes to the finally block and then executes the code after the block. On the otherhand, if the code exits the try block by means of a return; statement flow goes to the finally block and then back to main and executes the next method(2) call which complete as expected, printing 13234 because as mentioned no return; in the catch block.

In this example, if there is a return; statement in either the try(last line) or catch block the compiler is happy with the code after the finally block. But having a return; statement in both try and catch block the code after the finally block becomes unreachable. To me it seems that the compiler treats the return; statement in the try block in the manner(or almost) in which it treats the one in the catch block, that is an exception has been thrown/caught making the code after the finally bock unreachable. Removing the return; statement in the catch or the try blocks causes the code after finally to be reachable again. So I guess i'm asking in the context of this question, is how the compiler, the return; statement(s) along with the code find a working medium.

Sorry for the epic, hope it makes sense.

Regards
Walter

Online
admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by admin »

A return statement takes the flow of control back to the place from where that method was called. Any statement after the return statement will not be executed. An exception to this rule is the finally block. As I mentioned above, if you execute a return statement in a try block or a catch block, the finally block will be executed before the flow of control goes back to the caller. I hope this part is clear.

An unreachable statement is a separate issue. The compiler looks at the given piece of code and finds out all possible paths of execution, without executing the code of course. If it is able to determine that a certain line of code will not be executed in any of these possibilities, then it generates an error message saying that line is unreachable. If there is even one path of execution in which that line of code will get to execute, then the compiler cannot reject the code. I hope this part is also clear.

How you digest the above two rules is up to you. You can certainly think of it as the return statement changing the behavior of the try/catch/finally block or something else. But the easiest approach is usually to think like a compiler and execute each line of code mentally and see where that leads you.

Now, you should be able to deduce what happens if there is a return statement in try block as well as the associated catch block, and if there is a line of code after the try/catch (and finally) block. If you are not, please post the exact code that you have an issue with so that I can understand the cause of confusion.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 47
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by Javier »

Hi Enthuware!
I would like to know why in this code is not necessary to handle or declare the call to:
method(1);
method(2);
in main method.
Because method throws checked exception.
Thank you very much!

Online
admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by admin »

Because the part of the code the method that throws Exception is within a try block. This try block has a catch(Exception e) associated with it. So if the code throws an Exception, it will be caught by the catch block and the exception will not escape out of the method at all.
Therefore, as far as the caller of method(int i) is concerned, this method doesn't throw any exception at all. Thus, no need to declare the throws clause in this method.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 47
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by Javier »

Hi again!
So we could say that it´s only necessary to declare/handle the caller method when the method declares that is throwing at exception?
public static void method( int i) throws Exception { }
// So now, in main we would have to declare the Exception or handle the method(1) and method(2) with try-catch block.

Online
admin
Site Admin
Posts: 8792
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1254 :

Post by admin »

Yes, that is correct.
But I will suggest you to go through this topic from a good book before attempting mock exams because your question was very basic and it should have been cleared by the time you got to the mock exams.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: admin, Bing [Bot] and 1 guest