I don't know if this is the best Java can offer, but this is what I did some time ago.
At first, all the interesting activity that might crash was sent using the command template. This application consisted of getting the application server over the Internet, so a lot could go wrong. Exceptions were caught by the command manager and the corresponding result displayed to the user (usually a dialog box with an error message is displayed, after which a trip message is issued and an alarm message is sent).
Second, Swing used a custom event queue to catch any exceptions that occur in the event stream. I hope Java now has a better solution, but basically when the exception occurred, you had to check if your code was involved, otherwise some Swing errors could crash your application, which you don't like. And, of course, you had to check the recursion (the crash repeats over and over again when you try to display a message to the user).
By the way, most of any failure will keep your JVM, including due to memory errors, sufficient to send e-mail in most cases, since after an error from memory, as a rule, the error frees up enough stack (and therefore a lot), to allow further garbage collection and paste your code live. But in this case, you should still get out quickly. IDEA continues to work after a memory error, but often does not work properly. They would be better off, IMO.
You push a new queue with the next and subclass of EventQueue to associate your behavior.
Toolkit.getDefaultToolkit().getSystemEventQueue().push(newQueue);
source share