[Webinar] Streamline your web hosting managementRegister Today


Result Object

Posted on 1999-11-09
Medium Priority
Last Modified: 2008-03-06
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.
Question by:rzvika3

Expert Comment

ID: 2195403
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...

Author Comment

ID: 2195437
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.


Expert Comment

ID: 2195464
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...

Expert Comment

ID: 2196544

 nice comment by _lychee_

Accepted Solution

isoBoy earned 60 total points
ID: 2223861
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.

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 21 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question