Skip to content

Instantly share code, notes, and snippets.

@dannas
Last active February 23, 2017 14:18
Show Gist options
  • Save dannas/ee9f1f81576388836ecd15eea0247652 to your computer and use it in GitHub Desktop.
Save dannas/ee9f1f81576388836ecd15eea0247652 to your computer and use it in GitHub Desktop.
When will stack undwinding take place?
// When will the destructor of class C get called?
// 1. If we call exit?
// 2. If we call abort?
// 3. If we throw an exception?
// 4. If we throw an exception that won't be caught (comment out the try-catch statement)
// and thus invokes the terminate() handler?
#include <cstdlib>
#include <iostream>
using namespace std;
class C {
public:
C() { cout << "ctor\n"; }
~C() { cout << "dtor\n"; }
};
int f() {
C c;
// exit(0); // 1.
// abort(); // 2.
// throw 42; // 3.
}
int main() {
try { // 4.
f();
} catch (...) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment