fork(4) download
  1. #include <exception>
  2.  
  3. typedef enum foo_Result {
  4. FOO_OK,
  5. FOO_ERROR1,
  6. FOO_ERROR2,
  7. FOO_UNKNOWN
  8. } foo_Result;
  9.  
  10. struct MyException1 { };
  11. struct MyException2 { };
  12.  
  13. foo_Result lippincott()
  14. try
  15. {
  16. try
  17. {
  18. if (std::exception_ptr eptr = std::current_exception())
  19. {
  20. std::rethrow_exception(eptr);
  21. }
  22. else
  23. {
  24. return FOO_UNKNOWN;
  25. }
  26. }
  27. catch (const MyException1&)
  28. {
  29. return FOO_ERROR1;
  30. }
  31. catch (const MyException2&)
  32. {
  33. return FOO_ERROR2;
  34. }
  35. catch (...)
  36. {
  37. return FOO_UNKNOWN;
  38. }
  39. }
  40. catch (...)
  41. {
  42. return FOO_UNKNOWN;
  43. }
  44.  
  45. //////////////////////////
  46.  
  47. void Snafuscate(bool andDie)
  48. {
  49. if (andDie)
  50. {
  51. throw MyException1();
  52. }
  53. }
  54.  
  55. foo_Result foo_snafuscate(bool andDie)
  56. {
  57. try
  58. {
  59. Snafuscate(andDie);
  60. return FOO_OK;
  61. }
  62. catch (...)
  63. {
  64. return lippincott();
  65. }
  66. }
  67.  
  68. #include <iostream>
  69.  
  70. int main()
  71. {
  72. foo_Result r1 = foo_snafuscate(false);
  73. if (r1 == FOO_OK) std::cout << "r1 == FOO_OK\n";
  74.  
  75. foo_Result r2 = foo_snafuscate(true);
  76. if (r2 != FOO_OK) std::cout << "r2 != FOO_OK (r2 == " << r2 << ")\n";
  77. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
r1 == FOO_OK
r2 != FOO_OK (r2 == 1)