fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. //#include <unistd.h>
  5.  
  6. int open() {
  7. return 42;
  8. }
  9.  
  10. void close(int handle) {}
  11.  
  12. struct Context {
  13. int handle;
  14. std::vector<std::string> errors;
  15. };
  16.  
  17. class File {
  18. Context &ctx_;
  19. public:
  20. File(Context &ctx) : ctx_(ctx)
  21. {
  22. try {
  23. ctx.handle = open();
  24. } catch(...) {
  25. ctx_.errors.push_back("Unable to open file");
  26. }
  27. }
  28. ~File() {
  29. try {
  30. close(ctx_.handle);
  31. } catch(...) {
  32. ctx_.errors.push_back("An error occured");
  33. }
  34. }
  35. };
  36.  
  37. int main() {
  38. Context ctx;
  39. {
  40. File f(ctx);
  41. }
  42. for (auto e : ctx.errors)
  43. std::cout << e << std::endl;
  44. return 0;
  45. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
Standard output is empty