What will be printed in the following code?
#include <iostream>

class Locker {
    public:
        ~Locker() { throw 2; }
};

int main() {
    try {
        Locker locker;
        throw 1;
    } catch (int a) {
        std::cout << a;
    }
    return 0;
}
Explanation
If an object destructor is called due to stack spin-up as a result of calling an exception, then exiting such a destructor by throwing an exception is guaranteed to cause the call of the terminate() function. As a consequence, the program will crash with an error. If the exception excited in such a destructor is "caught", the call of terminate() will not be made.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes