Created
July 22, 2014 16:36
-
-
Save satveersm/197e62af5a11c48a4927 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
//Can we throw exception from destructor | |
//yes we can but its not advisable to do this | |
//Why | |
//Bcoz may be some other function make an object of this class on stack in try block | |
//and in that try block if exception generated then its unstable state and programme | |
// be go in abort | |
//Like here no issue in simple case | |
#include<iostream> | |
using namespace std; | |
class A | |
{ | |
public: | |
~A() | |
{ | |
try | |
{ | |
cout<<"\nA's dest called\n"; | |
throw 10; | |
} | |
catch(...) | |
{ | |
} | |
}; | |
}; | |
void OtherFunWithLeadAbort() | |
{ | |
try | |
{ | |
A a; | |
throw 10; | |
} | |
catch(...) | |
{ | |
cout<<"\nException handler here but it not reach here and abort called\n"; | |
} | |
} | |
int main() | |
{ | |
try | |
{ | |
A b; | |
} | |
catch(...) | |
{ | |
cout<<"\nException handled\n"; | |
} | |
cout<<"\nAfter b.fun()\n"; | |
OtherFunWithLeadAbort(); | |
return 0; | |
} | |
Output:- | |
A's dest called | |
After b.fun() | |
A's dest called | |
Exception handler here but it not reach here and abort called |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment