Solved

NEWBIE: Please advise on returning status of constructor

Posted on 2007-04-06
3
222 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
Comment Utility
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
Comment Utility
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
Comment Utility
Happy to help!
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now