Search code examples
c++raii

How to implement RAII if the resource acquisition could fail


I would like to implement a class with the help of RAII. The resources should be acquired in the constructor, but it's possible that the acquistition failed. I'll give an example in the following using FILE:

class file {
public:
    file(const char* filename) {
        file_ = fopen(filename, "w+");
        if(!file_) {
          // Okay
        }
        else {
          // ERROR
        }
    }

    ~file() {
        if (fclose(file_)) {
           // ERROR
        }
    }

    void write(const char* str) {
        if (EOF == fputs(str, file_)) {
            throw runtime_error("file write failure");
        }
    }
private:
    FILE* file_;
};

So, what's the best way to handle an error which is occurred if fopen returns NULL? Because it's the constructor I can't return also NULL.

I hope somebody can give me a hint how to handle such errors!

Thank you, best regards,

Flasher


Solution

  • The only way a constructor can report failure is by throwing an exception.

    Destructors, to the contrary, must not throw exceptions (if a destructor throws during stack unwinding, std::terminate is called, which ends the program by default).

    If destruction fails, you can

    • Swallow errors silently
    • Abort the program
    • Log the error and do either of the above.

    If you use RAII correctly, exceptions can traverse your code without damage.

    Example here:

    #include <cerrno>
    #include <cstring>
    #include <sstream>
    
    file::file(const char* filename) 
    {
        file_ = fopen(filename, "w+");
    
        if (!file_)
        {
            std::ostringstream os;
            os << "Cannot open " << filename << ": "
               << std::strerror(errno);
    
            throw std::runtime_error(os.str());
        }
    }
    
    file::~file()
    {
        fclose(file_);
    }
    

    Note that this code has many bugs: the fclose function can fail, and the failure may or may not be related to the closing (eg. some write errors are only reported when flushing at the close system call on POSIX systems). Please use iostreams for file I/O in C++, since they provide convenient abstractions over those concerns.