Suppose a class like this:
class A {
private:
QFile file;
public:
A::A(QFile file): file(file) {}
void doSomething() {
file.open(QIODevice::WriteOnly);
// ... do operations that can throw an exception
file.close();
}
}
if something occurs, the close() never calls it. The correct will be use try - finally, but C++ doesn't support it:
class A {
private:
QFile file;
public:
A::A(QFile file): file(file) {}
void doSomething() {
file.open(QIODevice::WriteOnly);
try {
// ... do operations that can throw an exception
}
finally {
file.close();
}
}
}
How can I do it on C++?