Possible Duplicate:
Throwing exceptions from constructors
When initializing an object - what would be the correct way to handle an issues that will prevent the object from fulfilling all of its responsibilities later on down the line.
For example, say a constructor initializes a set of config variables that are used for a remote connection. Not all config variables have been properly set and so later, when the object is used for that connection it will simply not work and ultimately throw an exception.
I can think of a couple of solutions to this
- The constructor throw an exception there and then
- The object provides a method for validating the config settings (this means clients of that object have the added task of checking valid state before using)
- Simply leave as is and let any exceptions be thrown, leaving clients to deal with them if they arise.
So, which of these options would be considered the best approach in the scenario provided, or if there is an alternative better way of handling this, what would that be.