fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <stdexcept>
  4. #include <functional>
  5. using namespace std;
  6.  
  7. void exceptionCatcher(std::function<void()> func)
  8. {
  9. try {
  10. func();
  11. }
  12. catch (const exception& e) {
  13. cout << "Caught exception\n";
  14. }
  15. }
  16.  
  17. int main()
  18. {
  19. string str("Hello, world");
  20.  
  21. thread myThread2(
  22. exceptionCatcher,
  23. [str]{
  24. cout << str << endl;
  25. throw exception();
  26. }
  27. );
  28.  
  29. myThread2.join();
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5248KB
stdin
Standard input is empty
stdout
Hello, world
Caught exception