55#include < iostream>
66#include < string_view>
77
8- void print_exit_status (std::string_view name, bool exit_status, bool did_throw)
9- {
8+ void print_exit_status (std::string_view name, bool exit_status, bool did_throw) {
109 std::cout << name << " :\n " ;
11- std::cout << " Exception thrown: " << (did_throw ? " yes" : " no" ) << " \n " ;
12- std::cout << " Exit status: " << (exit_status ? " finished" : " pending" ) << " \n\n " ;
10+ std::cout << " Throwed exception " << (did_throw ? " yes" : " no" ) << " \n " ;
11+ std::cout << " Exit status " << (exit_status ? " finished" : " pending" ) << " \n\n " ;
1312}
1413
1514// Randomly throw an exception (50% chance)
16- void maybe_throw ()
17- {
15+ void maybe_throw () {
1816 if (std::rand () >= RAND_MAX / 2 )
1917 throw std::exception{};
2018}
2119
22- int main ()
23- {
20+ int main () {
2421 bool exit_status{false }, did_throw{false };
2522
2623 // Manual handling at "end of scope"
2724 try {
2825 maybe_throw ();
2926 exit_status = true ;
30- }
31- catch (...) {
27+ } catch (...) {
3228 did_throw = true ;
3329 }
3430 print_exit_status (" Manual handling" , exit_status, did_throw);
@@ -38,8 +34,7 @@ int main()
3834 try {
3935 auto guard = std::experimental::scope_exit{[&] { exit_status = true ; }};
4036 maybe_throw ();
41- }
42- catch (...) {
37+ } catch (...) {
4338 did_throw = true ;
4439 }
4540 print_exit_status (" scope_exit" , exit_status, did_throw);
@@ -49,8 +44,7 @@ int main()
4944 try {
5045 auto guard = std::experimental::scope_fail{[&] { exit_status = true ; }};
5146 maybe_throw ();
52- }
53- catch (...) {
47+ } catch (...) {
5448 did_throw = true ;
5549 }
5650 print_exit_status (" scope_fail" , exit_status, did_throw);
@@ -60,8 +54,7 @@ int main()
6054 try {
6155 auto guard = std::experimental::scope_success{[&] { exit_status = true ; }};
6256 maybe_throw ();
63- }
64- catch (...) {
57+ } catch (...) {
6558 did_throw = true ;
6659 }
6760 print_exit_status (" scope_success" , exit_status, did_throw);
0 commit comments