The jAWTRobot library has a method called JVMAddShutdownHook() that allows you to handle graceful JVM shutdowns (that is, when the JVM shuts down under its own power, not when it's shut down by the OS or a Task Manager). This works in B4J UI and non-UI apps.
What are you trying to accomplish, overall? Are you trying to retrieve the stack trace of the Exception that is killing your app? The jAWTRobot allows you to redirect Standard Error to a text file. There's also some improved Exception handling features that will be released soon.