Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throwing IllegalArgumentException from DB interface

 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
would it be a good choice to throw IllegalArgumentException from interface methods for illegale arguments?

and how about throwing IllegalArgumentException in the constructor of the database entity (room, contractor, book etc) ?
 
Javier Corral
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you throw an IllegalArgumentException, the code using this class is not required to try/catch this exception, and this forces you to indicate in some place that this exception could be thrown, otherwise other programmer may get mistakes running his code.

I think if you add exceptions to the interface you should document it explicitly in your design decisions document, but I don't know if it can reduce your mark or even fail the assignment. However , you can wrap the exception in other exceptions allowed in the interface.
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply,
I thought about wrapping exceptions with allowed exceptions but these two have different meanings for example:
an update method that's supposed to recieve String[] of length 5, got only a String[] of length 2, the method can only throw DuplicateRecord and RecordNotFound...
these two exceptions don't explain what was the problem, the good thing is that IllegalArgumentException doesn't have to be declared in the method header(which means that I am not changing the interface).

see my point?
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should be fine throwing any unchecked exceptions as long as you specify that in your comments.

Check notes from Ken for example: http://www.coderanch.com/t/184523/java-developer-SCJD/certification/NX-Notes-design-passed
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic