Solved

NEWBIE: Please advise on returning status of constructor

Posted on 2007-04-06
3
227 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 155
hibernate example using maven 12 82
ejb stateless example 2 43
ejb mdb examples 1 21
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

733 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