Solved

NEWBIE: Please advise on returning status of constructor

Posted on 2007-04-06
3
225 Views
Last Modified: 2010-03-31
Dear Experts,

Given that constructors can't return anything, what is the preferred method of findiing out about their success?

As an example, I have a class that encapsulates a specific text file.  I try to load the text file in the constructor.  If the text file is not found, the class is worthless.  Ideally, my constructor would return a boolean, indicating it couldn't find the file.

Given that I can't do that, what's the next best thing?  I know I could set a class boolean, and then query that, but that seems awkward...

Thanks,
BrianMc1958
0
Comment
Question by:BrianMc1958
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 500 total points
ID: 18865527
Since the class is worthless, you probably want to throw an exception so the object creation never completes. Catch it in the methods that call that constructor and you'll be able to tell when the constructor failed.
0
 

Author Comment

by:BrianMc1958
ID: 18865549
To CPColin:  I should have thought of that!  Once you've explained it to me, it seems obvious.  Thanks very much.
0
 
LVL 14

Expert Comment

by:CPColin
ID: 18865556
Happy to help!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse package explorer vs project explorer view 2 131
java 8 lambda expresssions exception handling 3 90
xampp tool 12 49
hibernate jars 4 29
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

770 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