fork download
  1. #include <iostream>
  2. #include <exception>
  3. #include <stdexcept>
  4.  
  5. void universal_exception_handler(std::exception_ptr e)
  6. {
  7. try
  8. {
  9. std::rethrow_exception(e);
  10. }
  11. catch (const std::logic_error& e)
  12. {
  13. std::cout << "logic_error" << std::endl;
  14. }
  15. catch (const std::runtime_error& e)
  16. {
  17. std::cout << "runtime_error" << std::endl;
  18. }
  19. }
  20.  
  21. void foo()
  22. {
  23. throw std::logic_error{""};
  24. }
  25.  
  26. void bar()
  27. {
  28. throw std::runtime_error{""};
  29. }
  30.  
  31. int main()
  32. {
  33. try
  34. {
  35. foo();
  36. }
  37. catch (...)
  38. {
  39. universal_exception_handler(std::current_exception());
  40. }
  41.  
  42. try
  43. {
  44. bar();
  45. }
  46. catch (...)
  47. {
  48. universal_exception_handler(std::current_exception());
  49. }
  50. }
  51.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
logic_error
runtime_error