fork(1) download
  1. #include <iostream>
  2. #include <thread>
  3. #include <stdexcept>
  4. using namespace std;
  5.  
  6. template<typename Func>
  7. void exceptionCatcher(Func 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. auto func = [str]{
  22. cout << str << endl;
  23. throw exception();
  24. };
  25.  
  26. thread myThread2(
  27. exceptionCatcher<decltype(func)>,
  28. func
  29. );
  30.  
  31. myThread2.join();
  32.  
  33. return 0;
  34. }
Success #stdin #stdout 0.01s 5248KB
stdin
Standard input is empty
stdout
Hello, world
Caught exception