About Question com.enthuware.ets.scjp.v6.2.396 :

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

Moderator: admin

Post Reply
TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.396 :

Post by TwistedLizard »

If a synchronized method throws an exception in its execution, any locks acquired by the method are released automatically.
Am I correct in supposing that this only applies to a situation where the exception is thrown out of the method?

If an exception is thrown during a method's execution, but it's caught within that method, would any acquired locks would still be held?

For example

Code: Select all

class SynchronizedMethodTest{
  synchronized void m(){
    try{
      //do dangerous stuff
      throw new Exception();
    }catch(Exception e){}
    //method still has lock on class instance at this point? [A]
  }
  public static void main(String[] args){
    new SynchronizedMethodTest().m();
  }
}
at [A], is it correct that the method's lock on the class instance is still held?

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

Re: About Question com.enthuware.ets.scjp.v6.2.396 :

Post by admin »

Yes, that is correct. The exception has to get out of the method.
If you like our products and services, please help us by posting your review here.

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.396 :

Post by TwistedLizard »

Thanks. That makes sense.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 32 guests