Solved

NEWBIE: Please advise on returning status of constructor

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to convert String matching to regex in java 4 49
type mismatch (Object[] to double[] 4 33
iterator/ListIterator approach 17 39
running on tomcat not jboss eap 7.0 3 18
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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

856 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