Does a try-catch block catch segmentation fault errors?
I am reading a text file using the function given below but sometimes the file is empty and the
try/catch only catches C++ exceptions. Segmentation faults will only occur when your program does something illegal and invokes undefined behavior.
Remember that undefined behavior can manifest in different ways, including not crashing. You're lucky to have your program to crash to inform you that there's something you need to fix, but the program may not crash; you can't make your fallback code depend on the crash.
The appropriate thing to do is not to handle the crash like you would an exception, but instead to ensure that your program does not do anything illegal even when the input is not what you expect. In this case you need to change your code so that you know when the file is empty and need to provide another one.
There usually is a way to handle segmentation faults, but it's not intended to do the kind of recovery you're looking for. The mechanism is signals. You can install a signal handler which executes when a specified signal is raised, such as SIGSEGV for segmentation faults. However there's no requirement that such a signal will actually occur except when you explicitly raise it with std::raise. Also the things you can do in a signal handler when the signal is raised by the implementation are severely restricted;
If the signal occurs other than as the result of calling the abort or raise function, the behavior is undefined if the signal handler refers to any object with static storage duration other than by assigning a value to an object declared as volatile sig_atomic_t, or the signal handler calls any function in the standard library other than the abort function, the _Exit function, or the signal function with the first argument equal to the signal number corresponding to the signal that caused the invocation of the handler. Furthermore, if such a call to the signal function results in a SIG_ERR return, the value of errno is indeterminate.