aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes B&S:  Exceptions Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "B&S:  Exceptions Problem" Watch "B&S:  Exceptions Problem" New topic
Author

B&S: Exceptions Problem

Jack Gold
Ranch Hand

Joined: Feb 04, 2005
Posts: 85
Hello,
I am nearing completion and going back through realize I have been using a workaround. . .

The problem:
The DBAccess interface specifies that the createRecord() method will throw a DuplicateKeyException.

Due to my design which uses record number (physical file location) as the primary key, there will never be occasion for a DuplicateKeyException. Fine, I have documented this.

The bigger problem is that the createRecord() method may throw an IOException in the process of writing to the file, yet the interface does not allow this.

Soultions??

1) Catch IOException and do nothing. . . Document

2) Change DBAccess interface to allow throw IOException. I suspect we cannot modify this and pass.

3) Subclass Data. Have data implement DBAccess and subclass data with methods that throw appropriate exceptions. Yuk!


Any opinions? Suggestions? It became apparent a while ago that there are some underlying flaws in the design that the developer must work around.


SCJP 1.4<br />SCJD <br />SCWCD (Studying)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
One option, which many people have chosen, is to catch the IOException and then rethrow it as an exception defined by you say DataException which extends RuntimeException. A RuntimeException does not need to be declared in the throws section of a method declaration.
Jack Gold
Ranch Hand

Joined: Feb 04, 2005
Posts: 85
Originally posted by Matt Garner:
One option, which many people have chosen, is to catch the IOException and then rethrow it as an exception defined by you say DataException which extends RuntimeException. A RuntimeException does not need to be declared in the throws section of a method declaration.

Thanks!
Ara Tatous
Greenhorn

Joined: Jul 11, 2005
Posts: 16
Basically, What I deduct from DuplicateKeyException is that you can not create a record which already exist. This means that before creating the record you have to search to find a record with the criteria passed to createRecord() method. If you find a match then it means that you already have that record so you should throw DulplicateKeyException.
From project context from what I see in provided sample database file by Sun you can not have the business name in the same city twice record. But you can have the same business name in different cities record. So you can limit your search cirteria only to the first field, i.e. Business Name and Loction. If you find a match then you should throw DuplicateKeyException.
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
@ara

consequently thought, that's not enough. the major problem is that updateRecord() does not define any exception. how weired would that be, to deny creation but allow updating?

i decided to ignore the exception and documented this

greets,
jan
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
There are many cases where creating something is outside the scope of an application but updating is not.

In the scope of a reservation system for say a cruise liner the reservation application would be able to list a cabin as booked by a person for a period of time but be unable to create a new cabin.


42
Ara Tatous
Greenhorn

Joined: Jul 11, 2005
Posts: 16
Jan,

I think it was a mistake that you ignored the DuplicateKeyException.
Did you get the full score for database implementation?
The interface to access the database provided by Sun is a generic one.
You have to look at the context of the application also. For B&S assignment, the only thing that you can update a record is to update it's "owner" and that's when you make reservation. So either you can
create a new record with a unique Company Name and City Name or you can update an existing one's cutomer field.
Samuel Pessorrusso
Ranch Hand

Joined: Jul 21, 2005
Posts: 164
I think it was a mistake that you ignored the DuplicateKeyException.
Did you get the full score for database implementation?
The interface to access the database provided by Sun is a generic one.
You have to look at the context of the application also. For B&S assignment, the only thing that you can update a record is to update it's "owner" and that's when you make reservation. So either you can
create a new record with a unique Company Name and City Name or you can update an existing one's cutomer field.


If you think in such way, the SUN would not provide the create method in your data access interface. Your data access interface will be used by other applications.
I decided to ignore the DuplicateKeyException too.
 
jQuery in Action, 2nd edition
 
subject: B&S: Exceptions Problem