| Author |
Update method and exceptions
|
Nitti Lin
Ranch Hand
Joined: May 15, 2003
Posts: 39
|
|
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
|
SCJP, SCWCD, SCJD, (SCEA, MCSD.net)
|
 |
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10895
|
|
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
|
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
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.
|
"I'm not back." - Bill Harding, Twister
|
 |
Nitti Lin
Ranch Hand
Joined: May 15, 2003
Posts: 39
|
|
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
|
 |
Thomas Kijftenbelt
Ranch Hand
Joined: Feb 13, 2002
Posts: 73
|
|
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
Joined: May 15, 2003
Posts: 39
|
|
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 ]
|
 |
Billy Tsai
Ranch Hand
Joined: May 23, 2003
Posts: 1297
|
|
|
is that the last sentence of the server part for required interface just b4 Network Approaches heading?
|
BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
|
 |
Thomas Kijftenbelt
Ranch Hand
Joined: Feb 13, 2002
Posts: 73
|
|
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
Joined: May 15, 2003
Posts: 39
|
|
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
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
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
Joined: Feb 13, 2002
Posts: 73
|
|
Check, I have the DuplicateKeyException in the create() as well... and I don't see any use for it either. TK
|
 |
 |
|
|
subject: Update method and exceptions
|
|
|