Catch Segfault or any other errors/exceptions/signals in C++ like catching exceptions in Java

前端 未结 5 1488
长情又很酷
长情又很酷 2020-12-11 03:43

I wrote a Linux program based on a buggy open source library. This library sometimes triggers segfaults that I cannot control. And of course once the library has segfaults,

5条回答
  •  死守一世寂寞
    2020-12-11 03:55

    Unfortunately, you cannot make the program continue. The buggy code that resulted in SIGSEGV usually triggers undefined behaviour like dereferencing a null pointer or reading garbage memory. You cannot possibly continue if your code operates on invalid data.

    You can handle the signal, but the most you can do is dump the stack trace and die.

    C and C++ are inherently unsafe, you cannot handle errors triggered by undefined behaviour and let the program continue.

提交回复
热议问题