Reputation: 347
In java, if native code attempts to access protected memory, the OS will send a signal notifying the JVM that a segfault has occurred. My question is, why does the JVM decide to interpret this as fatal and crash, instead of throwing some sort of throwable (either exception or error). This feature makes it much harder to debug, or shutdown safely.
Upvotes: 1
Views: 3445
Reputation: 98332
Usually, when a C program segfaults, it is immediately terminated. The same happens to JVM when segfault occurs in native code. It is not safe to continue, since the native code could have left the application in an inconsistent state, and JVM does not know how to recover.
For example, a native function could have grabbed some native resource or a lock which could not be automatically released after a crash. JVM also does not know how to unwind native stack - it has control only over the Java stack. This is especially important for C++ code where stack unwinding implies calling destructors on stack-allocated objects.
However, it is possible to create your own signal handler for the native code. If you handle segmentation faults yourself, you may implement manual recovery from a failure and translate segfault to a Java exception, for example. This can be tricky though. See Integrating Signal and Exception Handling for details.
Upvotes: 5