/55
Exception specifications
• Declare which exceptions function might raise
• Part of function prototypes
void abc(int a) : throw(MathErr) {
...
}
• Not checked at compile time
• At run time,
–if an exception not in the list propagates out,
the unexpected exception is raised
/55
Failure in constructors:
• No return value is possible
• Use an “uninitialized flag”
• Defer work to an Init() function
Better: Throw an exception
/55
Failure in constructors...
If you constructor can’t complete, throw an
exception.
• Dtors for objects whose ctor didn’t complete
won’t be called.
• Clean up allocated resources before
throwing.
/55
Two stages construction
• Do normal work in ctor
–Initialize all member objects
–Initialize all primitive members
–Initialize all pointers to 0
–NEVER request any resource
• File
• Network connection
• Memory
• Do addition initialization work in Init()