Skip to content

Instantly share code, notes, and snippets.

@raptium
Created February 7, 2011 07:45
Show Gist options
  • Save raptium/814110 to your computer and use it in GitHub Desktop.
Save raptium/814110 to your computer and use it in GitHub Desktop.
#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