I’ve noticed RAII has been getting lots of attention on Stackoverflow, but in my circles (mostly C++) RAII is so obvious its like asking what’s a class or a destructor.
So I’m really curious if that’s because I’m surrounded daily, by hard-core C++ programmers, and RAII just isn’t that well known in general (including C++), or if all this questioning on Stackoverflow is due to the fact that I’m now in contact with programmers that didn’t grow up with C++, and in other languages people just don’t use/know about RAII?
For people who are commenting in this thread about RAII (resource acquisition is initialisation), here’s a motivational example.
Here, when a
StdioFileinstance is created, the resource (a file stream, in this case) is acquired; when it’s destroyed, the resource is released. There is notryorfinallyblock required; if the reading causes an exception,fcloseis called automatically, because it’s in the destructor.The destructor is guaranteed to be called when the function leaves
main, whether normally or by exception. In this case, the file stream is cleaned up. The world is safe once again. 😀