fork(7) download
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. class Resource
  7. {
  8. private:
  9. string _name;
  10. bool _exn;
  11. public:
  12. Resource(string name, bool exn) {
  13. _exn = exn;
  14. _name = name;
  15. cout << "Open " << _name << endl;
  16. }
  17. virtual ~Resource() {
  18. cout << "Close " << _name << endl;
  19. if (_exn)
  20. throw runtime_error("EXCEPTION: " + _name);
  21. }
  22. };
  23.  
  24. void test()
  25. {
  26. Resource a("A", false);
  27. Resource b("B", true);
  28. cout << "test" << endl;
  29. }
  30.  
  31. int main()
  32. {
  33. try {
  34. test();
  35. } catch (runtime_error& e) {
  36. cout << "CATCH: " << e.what() << endl;
  37. }
  38. return 0;
  39. }
Runtime error #stdin #stdout #stderr 0s 3460KB
stdin
Standard input is empty
stdout
Open A
Open B
test
Close B
stderr
terminate called after throwing an instance of 'std::runtime_error'
  what():  EXCEPTION: B