• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
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
 
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?
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic