Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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();
- }
- }
- 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();
- }
- }
- }
- void A::doSomething()
- {
- QFile file;
- file.open(...);
- // ...
- file.close(); // So you can check that everything when right.
- }
- class QFileWrapper
- {
- QFile* myFile;
- public:
- QFileWrapper( QFile* file ) : myFile( file ) {}
- ~QFileWrapper() { myFile->close(); }
- };
- try {
- /*do something that may throw an exception*/
- } catch (.../*the ellipsis means catch anything*/){
- /* any cleanup */
- throw; /*this rethrows the exception, not compulsory to include*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement