No, a segmentation fault is a signal, not an exception. The exceptions are manually launched by the program (either you, or a library), while the signals are generated by the operating system ( SIGSEGV
, SIGABRT
, SIGKILL
, etc ).
Exceptions are captured with blocks try
/ catch
, and signals with signal handlers
.
Signals can not be treated as exceptions because exceptions are thrown in a specific line of the program (where you put throw
), while signals can appear at any time (since they are like interruptions, for example, that the user press Ctrl-C also generates a signal, SIGINT
).
Exception handling involves adding, by the compiler, a certain control code in the final executable for the stack unwinding (the destruction of local objects as the exception traverses functions) and the logical try
/ catch
(the end of the stack unwinding ).
For signals, that control code can not be placed around any particular line of code because the signals are received "from the outside", as if a stone were thrown at you on the roof. They can happen at any time.
Even if you think that a SIGSEGV
happens, or is thrown , on the line that accesses an incorrect memory location, it does not really, or does not have to (as far as I know). The signal does not have to be received at the moment that memory is accessed (that is, it does not throw on the line that detects the situation), but it may come with a certain delay, or not never arrive if the memory accessed also belongs to your program (to another object, although in this case you would possibly observe UB
- undefined behavior - in execution).