-
-
Save raptium/814126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <stdexcept> | |
using std::cout; | |
using std::ostream; | |
using std::runtime_error; | |
struct auto_flush { | |
~auto_flush() { | |
cout << "\nflush called\n"; | |
cout.flush(); | |
} | |
}; | |
int g() | |
{ | |
throw runtime_error("dummy runtime error"); | |
} | |
int h() | |
{ | |
throw runtime_error("another dummy runtime error"); | |
return -1; | |
} | |
int f() | |
{ | |
try { | |
auto_flush af; | |
cout << "hello world"; | |
g(); | |
cout << h(); | |
} catch(...) { | |
// destructor of auto_flush will be called | |
// whenever an excpetion is catched or not | |
} | |
return 0; | |
} | |
int main() | |
{ | |
f(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment