Is it good or bad, to throw a plain java object as an Exception.
I am building a simple validation api, whose purpose is to transfer a set of validation errors from the business objects(Model) to the view layer. The idea is that the business object will validate() itself, populating a ValidationResult - a set of pair<fieldId, list<errorCode>. The validate() method will be called by the Controller, and if the set is not empty it will be fed to the View.
The View will present the list of errors in a div tag, and additionaly will color red the labels of culprit fields.
Additionaly in each busines method of the Model will call internally validate() and throw a runtime exception if the state is not valid:
BusinessObject::doSomething() { ValidationResult vr = this.validate(); if(!vr.isEmpty()) { throw new ValidationException("obj is not valid", vr); } ....// do something important. }
The question is whether it is wise to combine the ValidationResult and ValidationException:
class ValidationResult extends RuntimeException { .... }
I think there is no problem in throwing the POJO as exception. But ideally we should not add the error handling in the plain java object. You can create one seperate java error object and you can link the you POJO to this error object.