C doesn't really have exceptions. It has two things which play a similar role, but which are vastly different from one another: signals and error codes.
Let's consider error codes first. When a function call in C fails, it will typically signal this failure to the caller by returning an error code, or by returning a placeholder value (e.g, 0, -1, or NULL) and setting an error in errno
, or storing it in a location that can be retrieved by calling another method. No special effort is required to handle these exceptions. Simply check the return values of functions, e.g.
FILE *fh = fopen("example", "r");
if (fh == NULL) {
fprintf(stderr, "Couldn't open file: %s\n", strerror(errno));
return -1;
}
// do things with fh...
Signals, on the other hand, are used in C to denote really unusual situations, such as an attempt to execute invalid code or dereference a bad pointer, or other external conditions like the user pressing control-C to terminate your program. You can attempt to handle some such signals using the signal()
function, but the ones you've described, like a bad pointer, typically indicate that the process is screwed up in a rather permanent way, so allowing them to terminate the process is usually the correct solution.
If you are indeed running code that is prone to triggering segmentation faults, it would be wise to run it in a separate process, rather than allowing it to potentially corrupt the state of a Java runtime.