If you were dealing with languages, such as Java, C#, or Delphi, you obviously were using the try {} finally{} construction. Let me briefly describe to you what do these language constructions do.
When a program leaves the current scope via return or exception, code in the finally block is executed. This mechanism is used as a replacement for the RAII pattern:
// Some pseudo code (suspiciously similar to Java code)
try {
FileWriter f = new FileWriter("example_file.txt");
// Some code that may throw or return
// ...
} finally {
// Whatever happened in scope, this code will be executed
// and file will be correctly closed
if (f != null) {
f.close()
}
}
Is there a way to do such a thing in C++?