pastebin - collaborative debugging tool
rovema.kpaste.net RSS


rov_posixsignal2c++exception001.cpp - test whether a POSIX signal can be turned into a C++ exception
Posted by Anonymous on Tue 7th Apr 2026 14:59
raw | new post

  1. /*
  2.  * rov_posixsignal2c++exception001.cpp - test whether a POSIX signal can be
  3.  * turned into a C++ exception
  4.  *
  5.  * Compile with $ g++ -std=c++17 -g -Wall -Wextra rov_posixsignal2c++exception001.cpp #
  6.  *
  7.  * Written by Roland Mainz <roland.mainz@rovema.de>
  8.  */
  9.  
  10. #include <cstdio>
  11. #include <csignal>
  12. #include <exception>
  13. #include <string>
  14. #include <cstring>
  15. #include <stdexcept>
  16.  
  17. class posix_signal_exception : public std::exception {
  18. private:
  19.     int sig_num;
  20.     std::string msg;
  21.  
  22. public:
  23.     explicit posix_signal_exception(int signum) : sig_num(signum) {
  24.         /* FIXME: We should store the whole siginfo() data here */
  25.         msg = "Caught POSIX signal: " + std::to_string(signum) +
  26.               " (" + strsignal(signum) + ")";
  27.     }
  28.  
  29.     /*[[nodiscard]]*/ const char* what() const noexcept override {
  30.         return msg.c_str();
  31.     }
  32.  
  33.     /*[[nodiscard]]*/ int signum() const noexcept {
  34.         return sig_num;
  35.     }
  36. };
  37.  
  38. extern "C"
  39. void signal_handler(int signum) {
  40.     throw posix_signal_exception(signum);
  41. }
  42.  
  43. void setup_signal_handler(int signum) {
  44.     struct sigaction sa;
  45.     (void)memset(&sa, 0, sizeof(sa));
  46.  
  47.     sa.sa_handler = signal_handler;
  48.     (void)sigemptyset(&sa.sa_mask);
  49.  
  50.     sa.sa_flags = SA_NODEFER;
  51.  
  52.     if (sigaction(signum, &sa, NULL) == -1) {
  53.         throw std::runtime_error("setup_signal_handler: Failed to setup signal handler.");
  54.     }
  55. }
  56.  
  57. int main(int ac, char *av[])
  58. {
  59.     (void)ac;
  60.     (void)av;
  61.  
  62.     try {
  63.         setup_signal_handler(SIGFPE);
  64.  
  65.         (void)fprintf(stderr, "Signal handler installed. Triggering a divide-by-zero...\n");
  66.  
  67.         volatile int zero = 0;
  68.         int result = 10 / zero;
  69.        
  70.         (void)fprintf(stderr, "Result: %d\n", result);
  71.  
  72.     } catch (const posix_signal_exception &e) {
  73.         (void)fprintf(stderr, "#### posix_signal_exception caught\n");
  74.         (void)fprintf(stderr, "Message: '%s'\n", e.what());
  75.         (void)fprintf(stderr, "Signal Number: %d\n", e.signum());
  76.     } catch (const std::exception& e) {
  77.         (void)fprintf(stderr, "#### Standard exception caught: '%s'\n", e.what());
  78.     }
  79.  
  80.     return EXIT_SUCCESS;
  81. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at