Re: Segv Fault Handler for Java?
LaBird wrote:
Dear all,
I'd like to know if we can do some user-level signal handling like those
provided in the Linux + C/C++?
What I would like to do is to capture SEGV fault signal (or to make it
simpler, null pointer exception) which can happen anywhere in the Java user
program. Then a user-level function / code segment (the handler) is invoked
to do something that handles the fault, and finally program can resume from
the faulting point when the handler finishes.
Global try-catch is possible (see
Thread.setDefaultUncaughtExceptionHandler), but since the entire stack
is unwound in the process, it's only a last resort.
That said, I don't see why you would need such functionality.
NullPointerException and other subclasses of RuntimeException tend to be
indicative of programmer error (most of them can be avoided with runtime
checks like "if (foo != null)") that is rarely recoverable; subclasses
of Error are generally problems that the application can or should not
handle; the other checked exceptions should typically be handled in the
most local frame of reference possible, negating the need for such
global try-catch.
I don't have much experience in Java exception handling, but my pals told me
that using simple try-catch cannot achieve the above behavior. Is there any
other workaround? Thanks in advance!
My best advice is to think hard about what you want to do to make sure
that try-catch is unsuitable. If you really must have such global
nature, you can use the global try-catch.
If you REALLY want to use fault handling, you could go to JNI...
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth