aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes RecordNotFoundException and DuplicateKeyException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "RecordNotFoundException and DuplicateKeyException" Watch "RecordNotFoundException and DuplicateKeyException" New topic
Author

RecordNotFoundException and DuplicateKeyException

Lucas cotta
Greenhorn

Joined: Oct 10, 2011
Posts: 21
Hi all!!

What "parents" should I give to my implementation of RecordNotFoundException and DuplicateKeyException?
I'm thinking in IOException, is that ok?

Thanks in advance!
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2246

Howdy, Lucas!

What "parents" should I give to my implementation of RecordNotFoundException and DuplicateKeyException?
I'm thinking in IOException, is that ok?


Hum... let's think for a second. Is DuplicateKeyException an IOException? In other words, does the IS-A relationship apply here? If a record cannot be found, is it a problem regarding input/output?

Here's what I did: I created a DatabaseException, and had DuplicateKeyException and RecordNotFoundException extend it. I also had a few other exceptions to represent other situations, which also extended DatabaseException.


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Lucas cotta
Greenhorn

Joined: Oct 10, 2011
Posts: 21
Hi Roberto!!

I agree, thanks for the suggestion!
But did your DatabaseException extend IOException or Exception?
I'm asking all of this, because my adapted interface to deal with the RMI RemoteException, throws RemoteException in its methods.
So I guess I would have to extend IOException in the 2 mandatory Exceptions, or in the DatabaseException in your example...
Is that correct?

Thank you!!
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

Lucas cotta wrote:I'm asking all of this, because my adapted interface to deal with the RMI RemoteException, throws RemoteException in its methods.

I guess you are mixing two issues here. Ideally, there should be only one purpose to one exception(correct me if I'm wrong). RemoteException is meant for exceptions during RMI invocation. So, if you call a method, which throws RecordNotFoundException, then no matter if you call that method locally or remotely, it should throw only that exception (in case of exception of course).

You can further wrap that exception within RemoteException. Even better (which I did) - make your respective search method throw two exceptions - RemoteException and RecordNotFoundException.

What my point is - you can extend IOException for RecordNotFoundException, but RMI should not be reason for it. I simply extended Exception (super class of all exceptions) for those exception like RecordNotFoundException.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Lucas cotta
Greenhorn

Joined: Oct 10, 2011
Posts: 21
Hi Anayonkar!

Thank you for the reply!
Yes I am indeed confused with the RMI implementation.

But ok, I've realized that the mandatory exceptions aren't related with the RemoteException.

I'll change my question entirely:
From what I've searched, I understand that to implement a interface that extends Remote and DBMain, the methods called remotely must throw RemoteException.
But DBMain don't throw it in its methods. So I need to create an adapter that "translates" the DBMain with RemoteException in the methods.

My question:
In my controller class I have two situations, the standalone mode where I can use the original DBMain, and the network client mode that will use the AdaptedDBMain interface.
Should I create 2 instance variables, one for DBMain and another for AdaptedDBMain, and use them accordingly with the current running mode? Or maybe use just AdaptedDBMain, since in the end it will use the original Data class (but then the DBMain won't be used)?

Thanks!
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2246

Howdy, Lucas!

My DatabaseException extended Exception. A possible design of the business layer can be found here.

Now, for the other stuff, I don't know if you already saw this, but here is a paper I wrote about how to approach this certification and how to design the code. I think it may help you clearing your doubts!
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 4905
    
  10

That's bad design! And will result in a lot of ugly (and unnecessary) code (lots of ifs to see which instance variable you need to use). Both standalone mode and network mode should use the same interface.

I introduced a business service layer with just 2 methods (book & find). These methods could throw some business exceptions (room not found, room already booked,...) and also the RemoteException. This interface will have 2 implementations (1 for standalone, 1 for network) and both will use the Data class as an instance member (to invoke methods on). So you just need to develop 1 client and based on the startup parameter you'll use the appropriate (standalone or network) implementation to call methods on. That's a thin client approach (all logic happens on the server).
If you prefer a thick (fat) client approach you can do something similar, but your business service layer will contain similar methods than DBMain interface, but slightly adapted (throwing RemoteException, maybe using a POJO instead of String[],...). The rest will be the same as the thin client approach.

So you'll end up with a nice design and don't have unnecessary, ugly code.

Hope it helps!
Kind regards,
Roel

[edit] seems the great and wise Roberto was 15min faster, but both responses lead to similar results (that's because great minds think alike )


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2246

Roel De Nijs wrote:seems the great and wise Roberto was 15min faster, but both responses lead to similar results (that's because great minds think alike )


+1000!
Lucas cotta
Greenhorn

Joined: Oct 10, 2011
Posts: 21
Roel De Nijs wrote:
Hope it helps!


It helped!!
Thank you both, I'll take a look in your article Roberto!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: RecordNotFoundException and DuplicateKeyException
 
Similar Threads
URLyBird DBAccess exceptions
DuplicateKeyException - do I have to create a class for this?
urlyBird 1.2.3 - Exceptions
which one is the primary key?
Exceptions created as member classes of package