And yes, constructors are perfect examples of where to throw exceptions. If you can't construct the object, it is imperative that the calling code know about it; exceptions are the logical way to do this.
One thing, though -- if you're allocating memory via new or new[], you can have exception problems. It's as true in constructors as it is anywhere else. E.g. say you have this:
Code:
MyClass::MyClass{
myObjPtr1 = new MyObject();
myObjPtr2 = new MyObject();
}
If the second construction throws (the object that will be pointed at by myObjPtr2), then we leak memory because the first object is never deleted. A better way is to use smart pointers. Check out Boost. They have several classes of smart pointers. The following code is 100% exception safe:
Code:
MyClass::MyClass{
myObjSharedPtr1 = boost::shared_ptr(new MyObject());
myObjSharedPtr2 = boost::shared_ptr(new MyObject());
}
Exceptions are highly useful, and also highly misunderstood I'm afraid. Exception safety took a long time for even the most brilliant programmers to understand and define, but it's well known by now.