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.
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
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.
Additional note from me would be first try understanding or reading about Locks and then read synchronization because internally synchronization does similar to
Locks
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.
>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.
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.