4

Considering a small program like this :

int main()
{
    freopen("./stdout.log", "w", stdout);
    freopen("./stderr.log", "w", stderr);

    int a = 1 / 0;
}

and considering my program is ran by a third party software where I can't change neither how the program is launched nor the environment.

How to properly catch the Floating point exception (core dumped) message issued by the division by zero and any other messages that would still be printed on the tty ?

I've tried to search SO for similar answer but I might be just typing the wrong keywords as it seems a common practice.

Masadow
  • 762
  • 5
  • 15

1 Answers1

2

Matthieu Brucher's comment is correct:

You need to catch the signals, which is platform dependent.

From the text of the message, I infer that you may be running on a Linux platform. If so, you can catch the SIGFPE signal.

#include <stdexcept>
#include <signal.h>

// compile with -fnon-call-exceptions (for gcc)
signal(SIGFPE, [](int signum) { throw std::logic_error("FPE"); });

The linked answer has some C++ niceties such as using a std::shared_ptr for RAII management of the signal handler and mentions compiler flags needed for gcc to make that work.

The Linux Programming Interface book also has a pure-C example.


In Windows, you can use Structured Exception Handling (SEH) and the concept is similar (although the functions you need to call are not).


Note that in either case you're relying on platform-specific behavior not specified by C++ (division by zero is undefined behavior) so obviously this will not result in portable code.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • Super bad idea. Throwing `std::exception` from from signal handler is a recipe for a mighty disaster – SergeyA Nov 09 '18 at 15:43
  • @SergeyA I agree and edited the answer. That part came from the linked answer, and I left it alone assuming they knew something I didn't. – TypeIA Nov 09 '18 at 15:44
  • @SergeyA Actually, I am reverting the edit, as it seems my original thought (that they knew something I didn't) was accurate: gcc with `-fnon-call-exceptions` [does allow this to work](https://scc.ustc.edu.cn/zlsc/sugon/intel/compiler_c/main_cls/copts/ccpp_options/option_fnon_call_exceptions.htm). The documentation specifically mentions SIGFPE as a use case. – TypeIA Nov 09 '18 at 15:48
  • Than you need to add this information to the answer, and be super-specific about compiler. Alternatively, since your answer doesn't add much to the other answer you linked, you can delete your answer and close this question as a duplicate. As it stands, your answer is a (bastardized to the point of being incorrect) copy. – SergeyA Nov 09 '18 at 16:47
  • @Sergey I do mention the necessary gcc flag in the answer, and I don't see how it is "wrong" in any way. I augmented the other answer with information about other platforms, in a way that won't fit in a comment. I will leave the answer as-is; if mods wish to delete it or close, that's fine. I appreciate your input but please be respectful. – TypeIA Nov 09 '18 at 16:56