fork(10) download
  1. #include <exception>
  2. #include <iostream>
  3. #include <stdexcept>
  4.  
  5. template<typename E>
  6. void rethrow_unwrapped(const E& e)
  7. {
  8. try {
  9. std::rethrow_if_nested(e);
  10. } catch(const std::nested_exception& e) {
  11. rethrow_unwrapped(e);
  12. } catch(...) {
  13. throw;
  14. }
  15. }
  16.  
  17. void foo();
  18.  
  19. int main()
  20. {
  21. try {
  22. foo();
  23. } catch(const std::exception& e) {
  24. try {
  25. rethrow_unwrapped(e);
  26. } catch (const std::logic_error& e) {
  27. std::cout << "success! unwrapped to logic_error" << std::endl;
  28. }
  29. }
  30.  
  31. return 0;
  32. }
  33.  
  34. void never_called()
  35. {
  36. throw std::logic_error("never_called() was called");
  37. }
  38.  
  39. void throws_with_nested()
  40. {
  41. try {
  42. never_called();
  43. } catch (...) {
  44. std::throw_with_nested( std::runtime_error("something broke") );
  45. }
  46. }
  47.  
  48. void foo()
  49. {
  50. try {
  51. throws_with_nested();
  52. } catch (...) {
  53. std::throw_with_nested( std::runtime_error("oops") );
  54. }
  55. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
success! unwrapped to logic_error