• Post Reply Bookmark Topic Watch Topic
  • New Topic

readRecord return null or throw exception when a record is deleted?

 
William Dai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was doing final testing and I found an issue in my Data.java. Recently I changed my readRecord implementation from returning null to throw a RecordNotFoundException in the case when a record is marked as deleted. I just realized that I need to update other methods wherever they call readRecord.

Anyway, I am thinking just ask your guys opinion. During readRecord, if the record is marked as deleted, should we return null or throw RecordNotFoundException? I am inclined to throw exception in this case because it is not found.

During readRecord, if the record reading got some IO exception, I will throw RecordNotFoundException to wrap the inside exception.

Thanks to Roberto's Data testing tool. It prevents a lot of mistakes such as the above one.

Thanks,
William
 
Xin Gang Sun
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file.

So I think if the record is marked as deleted, you should throw a RecordNotFoundException and no return (must not return null). And I handle the IOException by printStackTrace() and return null because it is not a requirement.
That's my thought.

But I throw an IllegalArgumentException (A RuntimeException) in the updateRecord(long, String[], long) method. Is it OK?
 
Roel De Nijs
Sheriff
Posts: 10763
148
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William,

The quote from instructions.html (shown by Xin) is "should", not "must". So i think you have the choice to throw exception or to return null. If it was a must-requirement, you didn't have any choice.
In my solution I throw the RNFE (and I also throw a RNFE when find-method doesn't find any matching record).

I don't have any problem with the IOException, because i use a record cache and I only have read/write operations at the start and end of the program. But i think you should wrap these exceptions in a RuntimeException, like the IllegalStateException (instead of wrapping it in a RNFE), because it's not really a RNFE but an illegal state of your database file

Kind regards,
Roel
 
William Dai
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. That really clarified my doubt.

You are right that I should wrap IOException to a RuntimeException as that is really something severe happened and the program should not continue in such
state.

William
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!