The Artima Developer Community
Sponsored Link

Design Forum
Is it good or bad, to throw a Pojo as an Exception.

1 reply on 1 page. Most recent reply: Dec 26, 2007 2:15 AM by Sagar Jangle

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Sasoon Pundev

Posts: 3
Nickname: sasoon
Registered: Jun, 2004

Is it good or bad, to throw a Pojo as an Exception. Posted: Dec 24, 2006 2:45 AM
Reply to this message Reply
Advertisement
Hi,

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 {
....
}

in order to be able to do that:


BusinessObject::doSomething() {
ValidationResult vr = this.validate();
if(!vr.isEmpty()) {
throw vr;
}
....// do something important.
}


What do you think?

Best regards,
sasoon


Sagar Jangle

Posts: 1
Nickname: sagarpune
Registered: Dec, 2007

Re: Is it good or bad, to throw a Pojo as an Exception. Posted: Dec 26, 2007 2:15 AM
Reply to this message Reply
Hey,

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.

Thanks,
Sagar

Flat View: This topic has 1 reply on 1 page
Topic: How to identify classes in Object Oriented Design? Previous Topic   Next Topic Topic: Can any one suggest me??????

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use