• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Update method and exceptions

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In Data.java, the update method throws RecordNotFoundException. When writing to the disk, I use a try catch to catch the IOException and re-throw a RecordNotFoundException. The error message that I put to this re-throwing RecordNotFoundException is something like "System I/O error." My question is...
1) The error message I put is not really has anything to do with the "record not found." Does this consider as a bad design?
2) Can I throw not only the RecordNotFoundException, but also the IOException? This way, the exception class and its error message makes more sense to the caller, but DBMain interface only throws RecordNotFoundException.
Which way do you guys think is better? How do you guys handle it?
Thanks,
Nitti
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitti,
The error message I put is not really has anything to do with the "record not found." Does this consider as a bad design?
If you are only allowed to throw a RecordNotFoundException or a subclass of it, then I think what you are doing is OK.
As for the error message you are using in your Exception constructor, that in itself is not a problem. It can be valid to hide implementation details from the higher functions. Do they really care that there was an IOException, or do they only care about the fact that their record could not be updated? So hiding the IOException by throwing an Exception more suitable to the higher classes should be fine.
Can I throw not only the RecordNotFoundException, but also the IOException? This way, the exception class and its error message makes more sense to the caller, but DBMain interface only throws RecordNotFoundException.
If you are using JDK 1.4 then you can chain exceptions. This requires your exception to have a constructor that allows a parameter of a Throwable cause. That way the higher class can look at what exception caused the RecordNotFoundException (if one exists) if it is interested.
Regards, Andrew
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My own thoughts on this can be found here; you can also do a search for IOException in this forum, and find a number of past discussions on this and related topics.
 
Nitti Lin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys,
I should do a search before posting a question to avoid redundant topics.
I think that I will wrap the IOException into RecordNotFoundException.
Nitti
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitti,
Look through the post Jim suggests... I initially also thought of wrapping everything into RecordNotFound Exceptions; however you run into problemes with for example the FindByCriteria (which does not throw any exception).
I changed it, and wrap my IOExceptions into RuntimeExceptions, which I catch at the client side.
Greetings,
TK
 
Nitti Lin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi TK,
I have Contractor assignment. According to the instruction, find method throws RecordNotFoundException. I, now, wrap all the unrelated exceptions to either RecordNotFoundException or DuplicatedKeyException (for Create method), catch it on the client and show it to the user. It works pretty well. I really need to thank you guys for giving me such a good suggestions.
Nitti
[ June 03, 2003: Message edited by: Nitti Lin ]
 
Ranch Hand
Posts: 1327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is that the last sentence of the server part for required interface just b4 Network Approaches heading?
 
Thomas Kijftenbelt
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitti,
You're welcome... one remark however: I checked my spec. (also home contractor), and the signature for the find is as follows:

That's why I decided to wrap all my IOExceptions in RuntimeExceptions, which I catch at the client side.
Maybe your assignment is different, so make sure to check if the signature in your assignment is the same.
Good luck,
TK
 
Nitti Lin
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Kijftenbelt:



Hi TK,
Mine is
The is on the next line of public ...
I think we have a different version of requirement. My database file is db-2x3.db What is yours?
Thanks,
Nitti
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. I'm also doing NX-Contractors, with db file db-2x-1.db. My API is:
public int[] find(String[] criteria);
My version does not throw RecordNotFoundException, so it's a mix of the versions Thomas and Nitti describe. Not having a RecordNotFoundException makes sense to me - if there are no records matching the criteria, just return a empty array. As ordained in the Book of Joshua, Item 27. No silly exceptions needed.
However, elsewhere my API does have a completely unnecessary and meaningless DuplicateKeyException which is thrown by create(), but not update(). Any sensible interpretation of how or when to throw this would have to throw it from update() as well as create(), and I can't throw it from update(), so since they don't bother documenting what a "duplicate key" is anyway I have to assume that it's an error designed to confuse us (a test of "what do you do if the specifications are stupid", so to speak.) Perhaps the RecordNotFoundException is in a similar vein.
 
Thomas Kijftenbelt
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check,
I have the DuplicateKeyException in the create() as well... and I don't see any use for it either.
TK
 
reply
    Bookmark Topic Watch Topic
  • New Topic