class DBConnection {
public:
...
static DBConnection create();
void close();
};
// class to manage DBConnection
class DBConn {
public:
...
~DBConn() {
db.close();
}
private:
DBConnection db;
};
Terminate the program if close throws, typically by calling abort:
DBConn::~DBConn() {
try { db.close(); }
catch (...) {
std::abort();
}
}
Swallow the exception arising from the call to close
DBCoon::~DBCoon () {
try { db.close(); }
catch (...) {
}
}
because destructors that emit exceptions are dangerous, always running the risk of permature program terminate or undefined behavior. Telling clients to call close themselves does not impose a burden on them; it give them an opportunity to deal with errors they would otherwise have no chance to react to.
class DBCoon {
public:
...
void close() {
db.close();
closed = true;
}
~DBConn() {
if (!closed) {
try {
db.close();
}
}
catch(...) {
}
}
private:
DBConnection db;
bool closed;
};