Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. #include <err.h>
  2. #include <stdio.h>
  3. #include <setjmp.h>
  4. #include <signal.h>
  5.  
  6. static sigjmp_buf exception;
  7.  
  8. #define try if (!sigsetjmp(exception, 1))
  9. #define catch else
  10. #define throw siglongjmp(exception, 1)
  11.  
  12. void
  13. handler(int sig)
  14. {
  15. throw;
  16. }
  17.  
  18. void
  19. init_try(void)
  20. {
  21. struct sigaction sa;
  22.  
  23. sigemptyset(&sa.sa_mask);
  24. sa.sa_flags = 0;
  25. sa.sa_handler = handler;
  26. sigaction(SIGFPE, &sa, NULL);
  27. sigaction(SIGSEGV, &sa, NULL);
  28. }
  29.  
  30. int
  31. main()
  32. {
  33. int n = 0;
  34.  
  35. init_try();
  36.  
  37. try
  38. printf("%d\n", 1 / n);
  39. catch
  40. warnx("Look Ma, I divided by zero!");
  41.  
  42. try
  43. ((void (*)(void))NULL)();
  44. catch
  45. warnx("SIGSEGV you say? Well...");
  46.  
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement