<p>I'm working on a cross platform application in Java which currently works nicely on Windows, Linux and MacOS X. I'm trying to work out a nice way to do detection (and handling) of 'crashes'. Is there an easy, cross-platform way to detect 'crashes' in Java and to do something in response?</p>

<p>I guess by 'crashes' I mean uncaught exceptions. However the code does use some JNI so it'd be nice to be able to catch crashes from bad JNI code, but I have a feeling that's JVM specific.</p>

[{'answer_id': 61715, 'author': 'Free Wildebeest', 'author_id': 1849, 'author_profile': 'https://Stackoverflow.com/users/1849', 'pm_score': 2, 'selected': False, 'text': "<p>For handling uncaught exceptions you can provide a new ThreadGroup which provides an implementation of ThreadGroup.uncaughtException(...). You can then catch any uncaught exceptions and handle them appropriately (e.g. send a crash log home).</p>\n\n<p>I can't help you on the JNI front, there's probably a way using a native wrapper executable before calling the JVM, but that executable is going to need to know about all the possible JVMs it could be calling and how the indicate crashes and where crash logs are placed etc.</p>\n"}, {'answer_id': 61717, 'author': 'Evan', 'author_id': 6277, 'author_profile': 'https://Stackoverflow.com/users/6277', 'pm_score': 1, 'selected': False, 'text': '<p>Not sure if this is what you needing, but you can also detect if an exception has occurred from within your native code.  See <a href="http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp5234" rel="nofollow noreferrer">http://java.sun.com/javase/6/docs/technotes/guides/jni/spec/functions.html#wp5234</a> for more info.</p>\n'}, {'answer_id': 61806, 'author': 'David Crow', 'author_id': 2783, 'author_profile': 'https://Stackoverflow.com/users/2783', 'pm_score': 3, 'selected': True, 'text': '<p>For simple catch-all handling, you can use the following static method in <a href="http://java.sun.com/javase/6/docs/api/java/lang/Thread.html" rel="nofollow noreferrer">Thread</a>. From the Javadoc:</p>\n\n<blockquote>\n  <p>static void <a href="http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)" rel="nofollow noreferrer"><strong>setDefaultUncaughtExceptionHandler</strong></a>(<a href="http://java.sun.com/javase/6/docs/api/java/lang/Thread.UncaughtExceptionHandler.html" rel="nofollow noreferrer">Thread.UncaughtExceptionHandler</a> eh) <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set the default handler invoked when a thread abruptly terminates due to an uncaught exception, and no other handler has been defined for that thread.</p>\n</blockquote>\n\n<p>This is a very broad way to deal with errors or unchecked exceptions that may not be caught anywhere else.</p>\n\n<p><strong>Side-note:</strong> It\'s better if the code can catch, log and/or recover from exceptions closer to the source of the problem. I would reserve this kind of generalized crash handling for totally unrecoverable situations (i.e. subclasses of <a href="http://java.sun.com/javase/6/docs/api/java/lang/Error.html" rel="nofollow noreferrer">java.lang.Error</a>). Try to avoid the possibility of a <a href="http://java.sun.com/javase/6/docs/api/java/lang/RuntimeException.html" rel="nofollow noreferrer">RuntimeException</a> ever going completely uncaught, since it might be possible--and preferable--for the software to survive that.</p>\n'}]
['https://Stackoverflow.com/questions/61714', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1849/']