fork download
  1. #include <signal.h>
  2. #include <memory>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int main() {
  7. std::shared_ptr<void(int)> handler(
  8. signal(SIGFPE, [](int signum) {throw std::logic_error("FPE"); }),
  9. [](__sighandler_t f) { signal(SIGFPE, f); });
  10.  
  11. int i = 0;
  12.  
  13. cin >> i; // what if someone enters zero?
  14.  
  15. try {
  16. i = 1/i;
  17. }
  18. catch (std::logic_error e) {
  19. std::cerr << e.what();
  20. }
  21. }
Runtime error #stdin #stdout #stderr 0s 3416KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::logic_error'
  what():  FPE