88void print_exit_status (std::string_view name, bool exit_status, bool did_throw)
99{
1010 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 " ;
11+ std::cout << " Throwed exception " << (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,44 +24,52 @@ int main()
2424 bool exit_status{false }, did_throw{false };
2525
2626 // Manual handling at "end of scope"
27- try {
27+ try
28+ {
2829 maybe_throw ();
2930 exit_status = true ;
3031 }
31- catch (...) {
32+ catch (...)
33+ {
3234 did_throw = true ;
3335 }
3436 print_exit_status (" Manual handling" , exit_status, did_throw);
3537
3638 // Using scope_exit: runs on scope exit (success or exception)
3739 exit_status = did_throw = false ;
38- try {
40+ try
41+ {
3942 auto guard = std::experimental::scope_exit{[&] { exit_status = true ; }};
4043 maybe_throw ();
4144 }
42- catch (...) {
45+ catch (...)
46+ {
4347 did_throw = true ;
4448 }
4549 print_exit_status (" scope_exit" , exit_status, did_throw);
4650
4751 // Using scope_fail: runs only if an exception occurs
4852 exit_status = did_throw = false ;
49- try {
53+ try
54+ {
5055 auto guard = std::experimental::scope_fail{[&] { exit_status = true ; }};
5156 maybe_throw ();
5257 }
53- catch (...) {
58+ catch (...)
59+ {
5460 did_throw = true ;
5561 }
5662 print_exit_status (" scope_fail" , exit_status, did_throw);
5763
5864 // Using scope_success: runs only if no exception occurs
5965 exit_status = did_throw = false ;
60- try {
66+ try
67+ {
6168 auto guard = std::experimental::scope_success{[&] { exit_status = true ; }};
6269 maybe_throw ();
6370 }
64- catch (...) {
71+ catch (...)
72+ {
6573 did_throw = true ;
6674 }
6775 print_exit_status (" scope_success" , exit_status, did_throw);
0 commit comments