aspose file tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Exception to throw when unlocking a record that is not locked Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Reply Bookmark "Exception to throw when unlocking a record that is not locked" Watch "Exception to throw when unlocking a record that is not locked" New topic
Author

Exception to throw when unlocking a record that is not locked

Olu Shiyan
Ranch Hand

Joined: Jun 10, 2010
Posts: 56

I have read the discussion here about not throwing RecordNotFoundException from the method and it makes sense.

So firstly, what I've done is to throw a SecurityException if the record number specified is indeed locked and the cookie doesn't match that which was used to lock the record.

Secondly, if the record number specified does not exist in the 'lockedRecords' Map (meaning its not a locked record), I also throw a SecurityException with a message that tells the user that: 'You must first lock a record before you can unlock it.' Of course my lock() method throws RNFE as appropariate.

However, I'm wondering whether this second part is necessary or whether a SecurityException is the appropriate exception to throw if a user specifies a record number that is not locked (as described above)?


Suggestions? Confirmations? Criticisms?

Thanks
Olu

[Edit: changing subject line to make more sense]


SCJP 6, OCMJD6
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2212

Howdy, Olu!

Secondly, if the record number specified does not exist in the 'lockedRecords' Map (meaning its not a locked record), I also throw a SecurityException with a message that tells the user that: 'You must first lock a record before you can unlock it.' Of course my lock() method throws RNFE as appropariate.


Well champ, I'd say that a more appropriate exception to be thrown in this case would be the IllegalStateException, because someone is trying to unlock a record whose number should be in your lockedRecords map, and it isn't, so it is in a illegal state. If I make no mistake, my good buddy Roel also took this approach.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Olu Shiyan
Ranch Hand

Joined: Jun 10, 2010
Posts: 56

Thanks! Roberto
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4351

Hi Olu,

I think it depends on the method signatures in your interface. If I'm not mistaken there are some version where update/delete/unlock methods have a "throws SecurityException" clause. So in this case I would throw a SecurityException. My interface didn't have such method signatures, so I opted to simply throw an IllegalStateException.

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Olu Shiyan
Ranch Hand

Joined: Jun 10, 2010
Posts: 56


Hi Roel,

The unlock() method in the Sun interface I received throws SecurityException which is why I initially coded it like I explained in my first post. I think the scenario of invoking the unlock method with a record number that's not locked is some kind of Security Exception too. However, Roberto's explanation about the Map seems to make sense as well. Now Im slightly confused about which one to choose?

Another thing is the InterruptedException which can be thrown from the invocation of wait() in the lock method. At present I've rethrown it as a RuntimeException but I wonder whether that's the right thing to do?


Cheers
Olu
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 220

Hi Olu,

I do what my interface says:



Please try to use the search engine of the SCJD forum. Many of these questions have already been answered.
Look here regarding InterruptedException in the lock() method.


HTH,


Carlos.


SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2212

Howdy, Olu!

Well, for the original question, I'd say that it would be more appropriate to throw IllegalStateException because the object would then be in a illegal state. The SecurityException would be more appropriate in a case, say, where client A locks record 1 and client B tries to unlock record 1. In other words, where the record is locked, but the cookie received does not match the one that was assigned to the client who originally locked the record. But, if you choose to throw SecurityException in this case, you can mention it in your choices.txt file and say that in your point of view, trying to unlock a record that was not locked is a security violation.

For the second question, I'd say that a good way out would be to wrap the InterruptedException in a RuntimeException.
Olu Shiyan
Ranch Hand

Joined: Jun 10, 2010
Posts: 56



Thanks guys for the responses.

Roberto I think you are right about IllegalStateException being more appropriate for the above mentioned scenario. However, for this assignment, to be on the safe side, I think I'll just throw a SecurityException since it's in the method declartion, although throwing an IllegalStateException won't hurt since it's a RuntimeException subclass.

Carlos: Yes I did read that thread related to InterruptedException, thanks. I probably shouldn't have asked the same question. As for the question about the InterruptedException, I currently have it wrapped with a RuntimeException and rethrown. I think this will do.


Thanks
Olu
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 220

In my personal opinion it is more elegant and cleaner when a thread calls the lock() method
and is waiting in a loop while the record is locked and it catches an InterruptedException to restore the
interrupted status to not swallow the interrupt, as illustrated in the
book "Java Concurrency in Practice" by Brian Goetz Chapter 5 Section 5.4 Listing
5.10 and in http://www.ibm.com/developerworks/java/library/j-jtp05236.html.


HTH,


Carlos.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception to throw when unlocking a record that is not locked
 
Similar Threads
A question about throwing SecurityExeption in update method
Why throw an RecordNotFoundException in your unlock method?
B&S 2.1.1: lock(), delete() and unlock() confusion
My locking model
Locking strategy with singleton