Result Object

It happens a lot of times that we call a method for doing something and we want to know both if the method operation result was ok (and if so, to know the returned value) or not ok.
I thought of creating a Result Class that will contain int (represents the result: success/failure) and an error message that will contain the error message in case of error and last, an Object that contain the Object result in case it was ok.
Is it ok to do so?
is it clear to understand the Result class' purpose and data members?
please comment.
Thank you very much.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

if your code doesn't produce 'error' results very often, i think the exception model is much clearer in terms of semantics... and it forces u to handle it...
rzvika3Author Commented:
I have this case:
I have created a class C, and the creation of an instance of it can throw a lot of Exception types.
I want to do a static method in C that either creates a C object or tells an error if something was wrong.
I think that using Result object can be useful in this case.

Exception itself has a message facility... so i still dun get the pt of the Result object...

Result tells if there's an error...
Exception tells if there's an error

Result contains error message if error
Exception can contain error msg if error

Result returns whatever
No exception is thrown, and all is well...

 nice comment by _lychee_
Definitely use exceptions in, uh, exceptional circumstances. If you expect the "error" a lot, maybe not (as lychee noted) because exceptions are moderately expensive.
Another technique is to return a result object on sucess, null on failure.  Called method handles the error mesageing, as there's no way to pass it back.  This is ok technique for routine failures.

So you have to ask yourself: how often will this fail, and who needs to know the exact reason?  Try to make your objects encapsolate their behavior if you can, not "pass the buck" to the calling routine.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.