fork download
  1. #include <memory>
  2. #include <stdexcept>
  3. #include <iostream>
  4.  
  5. struct Object
  6. {
  7. Object() { std::cout << "An Object is created!\n"; }
  8. ~Object() { std::cout << "An Object is destroyed!\n"; }
  9. };
  10.  
  11. class MyClass
  12. {
  13. std::unique_ptr<Object> _object;
  14.  
  15. void methodThatWillCauseException()
  16. {
  17. throw std::runtime_error("ERROR: gratuitous exception");
  18. }
  19. public:
  20.  
  21. MyClass() : _object(new Object)
  22. {
  23. methodThatWillCauseException();
  24. }
  25. };
  26.  
  27. int main()
  28. {
  29. try {
  30. MyClass Instance;
  31. }
  32. catch (std::exception& ex)
  33. {
  34. std::cout << ex.what() << '\n';
  35. }
  36. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
An Object is created!
An Object is destroyed!
ERROR: gratuitous exception