CJCoding With Joseph
15free / daySign inCreate account
← Back to Question List

Select topics to narrow your question pool, then enable Random to jump to a random question matching your filters.

Topics:
C++ Quiz #138

No finally Keyword in C++

easyExceptions

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>
3 
4struct Guard {
5 ~Guard() { std::cout << "cleanup\n"; }
6};
7 
8void work() {
9 Guard g;
10 std::cout << "work\n";
11 throw std::runtime_error("failed");
12}
13 
14int main() {
15 try {
16 work();
17 } catch (const std::exception&) {
18 std::cout << "caught\n";
19 }
20 return 0;
21}