About Question enthuware.ocpjp.v7.2.1423 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

About Question enthuware.ocpjp.v7.2.1423 :

Post by Danny Sheridan »

I answered false to this question although aware of the abrupt completion scenario.
Here's my reasoning:
The explanation is correct for the abrupt completion of synchronized code.
But the question relates to a synchronized method which 'throws an exception in its execution'.
As 'throws out of the method' or 'an unhandled exception'
is not specified could not this just as well be a handled exception?

For example, completingMethod() completes normally here

Code: Select all

package study.ocp2;

public class SynchronizedAbruptCompletion {
	public static void main(String[] args) {
		completingMethod();
	}

	static synchronized void completingMethod() {
		try {
			throw new Exception();
		} catch (Exception e) {
			System.out.println("Handled "+ e.getClass().getName()); 
		}
		System.out.println("Further operations"); 
	}
}

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

Re: About Question enthuware.ocpjp.v7.2.1423 :

Post by admin »

Yes, it is possible to interpret it that way. But in the exam you may find similarly worded questions.
Also, if you think about it, in your code the method doesn't really throw any exception. The code inside the method throws an exception. When you say that a method throws an exception, it is implied that the exception is thrown out by the method.

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

boyonsea
Posts: 16
Joined: Fri Sep 04, 2015 5:06 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1423 :

Post by boyonsea »

Completely agree with the OP

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1423 :

Post by jagoneye »

Additional note from me would be first try understanding or reading about Locks and then read synchronization because internally synchronization does similar to
Locks :)

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1423 :

Post by Mark7777 »

So let me get this straight. The question doesn't pertain to an exception caught and handled in the catch clause where the program might continue on? In that case, is the lock released/surrendered? That would make for complicated programming, it seems. Or, is the question limited to an exception thrown out of the method and not handled, in which case the "this" lock is released? I assumed both scenarios. When a method declares "throws Exception" it always seemed to me that the method throws the exception. Tricky language.

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

Re: About Question enthuware.ocpjp.v7.2.1423 :

Post by admin »

>When a method declares "throws Exception" it always seemed to me that the method throws the exception.
Very sorry to say this but I do not expect this statement from an OCPJP aspirant, given that they have already cleared the OCAJP! Just think about it for a second that if this were the case, why would anyone write any code inside a method that has a throws clause? The only statement in such a method would be "throw new <the_exception_mentioned_in_declaration>;".

The throws clause in a method declaration only means that the method MAY throw that exception to the caller. The purpose of the declaration is to make the caller aware of this possibility so that it can prepare itself to take appropriate action.

If you look at the question from this perspective, you will see that the question is actually quite clear and not tricky at all.

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

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1423 :

Post by Mark7777 »

Thanks, Paul. That makes sense.

maderkse
Posts: 5
Joined: Thu Sep 13, 2018 3:46 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1423 :

Post by maderkse »

Hi,

I answered false... The question says "the lock acquired by the method". But shouldn't that be "the lock acquired by the current Thread"?

Regards,
Maarten

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

Re: About Question enthuware.ocpjp.v7.2.1423 :

Post by admin »

Actually, locks are always acquired by threads but the problem statement is quite clear that it is talking about the lock associated with a method due to the method being synchronized. Also, a thread may have acquired other locks but those locks are not released.

I have updated the problem statement to make it more clear.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests