I have an ongoing debate with one of my developers regarding how to
appropriately perform data validations within a business class.  The debate
essentially focuses on whether the set() methods of a business class, upon
discovering invalid data, should throw Java's IllegalArgumentException (a
RuntimeException) or should throw ValidationException (a class we wrote,
which extends Exception).

His position is that throwing a ValidationException from every set() method
forces you to write a lot of code catching ValidationExceptions, which you
may not be able to do anything about anyway.  Catching the exception is
therefore of limited use, so why not throw the IllegalArgumentException
which does not need to be caught?  If you want to catch it, you still can.

My feeling is that data is most likely to be invalid after a user has
entered it, and therefore you want to force the ValidationException to be
caught so that a meaningful message can be displayed to the user regarding
what they did wrong.  The fact that a RuntimeException doesn't need to be
caught means that they almost never are, which in turn means that when they
occur, the only way that they are dealt with is by the JVM printing the
stack trace in the console.  Not the most effectively way to communicate an
exception message to the user.

The following link, which someone posted earlier to this list, seems to back
me up to a certain degree:

        
http://java.sun.com/docs/books/tutorial/essential/exceptions/runtime.html

What are other people's thoughts on this matter?  Also the JavaDocs for the
IllegalArgumentException itself are pretty vague, stating only "Thrown to
indicate that a method has been passed an illegal or inappropriate
argument."
Thanks,
Dale 

Dale V. Georg              
Product Manager
Kronos Enterprise Scheduler
Indus Consultancy Services
(201) 261-3100 x229



To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to