fork download
  1. #include <iostream>
  2. #include <ostream>
  3. #include <functional>
  4. #include <exception>
  5. using namespace std;
  6.  
  7. // Wrong scope(failure)
  8. class FailBlockT
  9. {
  10. typedef function<void()> T;
  11. public:
  12. T t;
  13. FailBlockT(T t)
  14. {
  15. this->t=t;
  16. }
  17. ~FailBlockT()
  18. {
  19. if (std::uncaught_exception())
  20. {
  21. t();
  22. }
  23. }
  24. };
  25.  
  26. struct Test
  27. {
  28. ~Test()
  29. {
  30. try
  31. {
  32. FailBlockT f([]()
  33. {
  34. cout << "failure" << endl;
  35. });
  36. // there is no any exception here, but "failure" is printed.
  37. // See output below
  38. }
  39. catch(...)
  40. {
  41. cout << "some exception" << endl;
  42. }
  43. }
  44. };
  45.  
  46. int main()
  47. {
  48. try
  49. {
  50. Test t;
  51. throw 1;
  52. }
  53. catch(int){}
  54. return 0;
  55. }
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
failure