Since segfaults are not caused (directly) the the software, but rather by the processor detecting that you are trying to access invalid memory (or access memory in an invalid way - e.g writing to memory that is write-protected, executing memory that isn't supposed to be executed, etc), it is not "catchable" with try/catch
, which is designed to catch software that throws an exception. They are both called exceptions, but they originate at different levels of the software/hardware of the system.
Technically, you can catch segfaults with a signal handler for SIGSEGV
. However, as Ivaylo explains, it's is not, typically, allowed to just "try again" if you get a segfault. The signal hander for SIGSEGV
is allowed to longjmp
or exit
, but shouldn't just return.
Read more about signals here:
http://www.alexonlinux.com/signal-handling-in-linux
Typical C++ exceptions (result of throw
) can be retried without problem (of course, the same exception may be thrown again, of course.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…