RAII stands for Resource Acquisition Is Initialization. It's a useful way of handling resources, such as memory, fonts, and files to ensure that they are released when no longer needed or when a process terminates due to an error. Resources are generally tied to objects. A file handling class will probably require a file handle to access a file for opening, reading or writing the file. Once the object is finished with the file, the resource must be released. The basic problem RAII solves is that if the file handling object terminates, say due to an exception, then the resource must be freed if it has been acquired. After an exception in C++, the only code that can run is the object's destructor. Constructors can fail as well so it makes sense to return an exception. READ NOW |