Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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

Database Exception - should it be made a runtime exception?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
Im wondering if Database Exception should be made a runtime exception instead of a checked exception. Database exception indicates a corruption of the database or invalid argument passed to one of the database methods (eg.getRecord(int recNum)). This a a result of a programming error.
It states in efective java programming that "you should use checked exceptions for conditions which the caller can resonably expect to recover" and "Use runtime exceptions to indicate programming errors". Therefore, Database Exception should extend runtime exception.
Any thoughts on this greatly appreciated?
Thanks,
Richard Walter
 
Richard Walter
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any comments?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My take on it is that it should be a checked exception based on the way it is being used in other places in the database. If you look at the add() or find() methods, you'll see that it is being used as an extension of IOException. The other way it is being used is when the user supplies an invalid parameter (as you pointed out in getRecord(). This could be prevented by reading the comments in the API and implementing the appropriate checks, but it's a stretch to make sure the programmer deals with it.
The comment that seals it for me comes from Effective Java as well...
[On making exceptions checked]
"The burden is justified if the exceptional condition cannot be prevented by proper use of the API and the programmer can take some useful action once confronted with the exception."
In this case the term "proper" carries all the weight. Based only on the signature of the method, passing an int with the value of 0 to the getRecord() method is perfectly fine. The definition of API that Joshua uses also doesn't mention the comments as being part of the API. So I'd say that passing 0 is "proper" use of the API.
As for the second part, being able to do something useful when confronted with the exception, as a programmer I could possibly display a dialog saying they had chosen an invalid record and continue, as opposed to doing nothing which could be very confusing to a user who is trying to display record 0 and sees no response.
That's my opinion. Hope it helps.
- James
 
Hey, check out my mega multi devastator cannon. It's wicked. It makes this tiny ad look weak:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic