Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NEWBIE: Please advise on returning status of constructor

Posted on 2007-04-06
3
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
CPColin earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

670 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