Why C++ doesn't have the finally keyword

Consider again this snippet of code from the preceding section's "pre-modern" code sample:

    try {      use(*wh);    } catch (...) {      delete wh;      throw;    }    delete wh;

In other languages such as Java and Python, these semantics might be expressed more compactly using the finally keyword:

    try {      use(*wh);    } finally {      delete wh;    }

C++ doesn't have the finally keyword, and shows no signs that it will ever enter the language. This is simply due to a philosophical difference between C++ and those other languages: the C++ philosophy is that if you're concerned with enforcing some invariant--such as "this pointer shall always be freed at the end of this block, no matter how we get there"--then you shouldn't ever be writing ...

Get Mastering the C++17 STL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.