Correct lock structure

Below is the canonical form using a lock.

Lock lock = new SomeLock();
lock.lock();
try {
	// do your work here
} finally {
	lock.unlock();
}
  1. Lock should be unlocked in finally in case exception is thrown in the try block. Don't forget to unlock.
  2. lock.lock() lies outside of try block since the lock() method could throw exception. If exception is thrown, the lock is not acquired, lock.unlock() in the finally will throw exception(e.g, IllegalMonitorStateException) and the real exception during lock is swallowed.