← Back to Question List
C++ Quiz #138
No finally Keyword in C++
C++ has no finally keyword. What guarantees that cleanup is printed in this program even though work() throws?
📄 Code
1#include <iostream>2#include <stdexcept>34struct Guard {5 ~Guard() { std::cout << "cleanup\n"; }6};78void work() {9 Guard g;10 std::cout << "work\n";11 throw std::runtime_error("failed");12}1314int main() {15 try {16 work();17 } catch (const std::exception&) {18 std::cout << "caught\n";19 }20 return 0;21}