• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RecordNotFoundException problem!

 
xi brian
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i.e
public String [] readRecord(long recNo)
throws RecordNotFoundException;

the readRecord must be in "try {} catch(RecordNotFoundException e){}"


however, in my code, i used to arraylist to store all the record, and whenever, program calls readRecord, it will reads data from arraylist, so RecordNotFoundException could never happen since i only read the number of record < arraylist.length. therefore, what's the point here to try and catch the RecordNotFoundException

thank you in advance
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, my buddy.

I think it should be something like this (for example):

you have Map<Integer, YourBean> map = new HashMap<Integer, YourBean>();

this stores the record number and your bean (key and value).
In your read method, you can say:

if (!map.containsKey(recordNumber)) {
throw new RecordNotFoundException();
}

Or, you can say:

List<YourBean> list = new ArrayList<YourBean>();

in your read method:

if (recordNumber > list.size()){
throw new RecordNotFoundException();
}

This is just a desing choice. If you choose not to throw an exception, just document, even though I think it is better to throw the RecordNotFoundExceptions in all methods that declare it in its signature, because the assignment says that "any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file".
[ March 24, 2008: Message edited by: Roberto Perillo ]
 
mohamed sulibi
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi;

what happen if the another user delete record number 2 and there are 100 record in the database and the user want the record 2 , i think you MUST
MUST throw RecrodNotFoundException.

Best Regards.
Mohamed Darim.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, buddies.

You know, I was thinking about keeping the records in a List structure. The problem is that, if record #2 is deleted, the List won't be able to say that, because since it is organized by indexes, when record #2 is deleted, record #3 will be #2, and it will look like record #2 was not deleted, then RecordNotFoundException will never be thrown... only in this situation -> if (recordNumber > list.size())... in this assignment, we don't get to insert or delete records, so it would be ok to keep these records in a List structure, but we have to think about future changes (tomorrow we could be providing these functionalities), so in my opinion, it is better to store the dataase records in a Map<Integer, YourBean> structure rather than in a List<YourBean> structure.

I hope everybody has a good day!
[ March 25, 2008: Message edited by: Roberto Perillo ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic