• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

B&S: Exceptions Problem

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ara Tatous
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
rubbery bacon. rubbery tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic