88void print_exit_status (std::string_view name, bool exit_status, bool did_throw)
99{
1010 std::cout << name << " :\n " ;
11- std::cout << " Throwed exception " << (did_throw ? " yes" : " no" ) << " \n " ;
12- std::cout << " Exit status " << (exit_status ? " finished" : " pending" ) << " \n\n " ;
11+ std::cout << " Exception thrown: " << (did_throw ? " yes" : " no" ) << " \n " ;
12+ std::cout << " Exit status: " << (exit_status ? " finished" : " pending" ) << " \n\n " ;
1313}
1414
1515// Randomly throw an exception (50% chance)
@@ -24,52 +24,44 @@ int main()
2424 bool exit_status{false }, did_throw{false };
2525
2626 // Manual handling at "end of scope"
27- try
28- {
27+ try {
2928 maybe_throw ();
3029 exit_status = true ;
3130 }
32- catch (...)
33- {
31+ catch (...) {
3432 did_throw = true ;
3533 }
3634 print_exit_status (" Manual handling" , exit_status, did_throw);
3735
3836 // Using scope_exit: runs on scope exit (success or exception)
3937 exit_status = did_throw = false ;
40- try
41- {
38+ try {
4239 auto guard = std::experimental::scope_exit{[&] { exit_status = true ; }};
4340 maybe_throw ();
4441 }
45- catch (...)
46- {
42+ catch (...) {
4743 did_throw = true ;
4844 }
4945 print_exit_status (" scope_exit" , exit_status, did_throw);
5046
5147 // Using scope_fail: runs only if an exception occurs
5248 exit_status = did_throw = false ;
53- try
54- {
49+ try {
5550 auto guard = std::experimental::scope_fail{[&] { exit_status = true ; }};
5651 maybe_throw ();
5752 }
58- catch (...)
59- {
53+ catch (...) {
6054 did_throw = true ;
6155 }
6256 print_exit_status (" scope_fail" , exit_status, did_throw);
6357
6458 // Using scope_success: runs only if no exception occurs
6559 exit_status = did_throw = false ;
66- try
67- {
60+ try {
6861 auto guard = std::experimental::scope_success{[&] { exit_status = true ; }};
6962 maybe_throw ();
7063 }
71- catch (...)
72- {
64+ catch (...) {
7365 did_throw = true ;
7466 }
7567 print_exit_status (" scope_success" , exit_status, did_throw);
0 commit comments