Java – does any program recognize that it crashed last time?

What is the best way to let the (Java) program recognize the crash when it was last run, and display a message "it looks like this program crashed last time. Please report this problem here: bla@foo.com. ……”

Is there any recommended method? (bad?) My idea is:

>Let the program store the temporary key file at startup and then delete it at periodic shutdown This message is displayed if the file exists at startup. > Identify the deadlock and store the "error file" in this case If there is an "error file" at startup, an error message is displayed and the file is moved to an archive or similar

Solution

Java programs crash for three reasons:

>Unhandled runtimeException This can be easily solved through try catch in main. > Unhandled error These are rare, but can also be mainly captured I usually mainly catch throwable Please refer to the template below. > If you use threads, see thread setDefaultUncaughtExceptionHandler(). > An error in the virtual machine, a program killed by a user, or a violent shutdown of hardware These will result in a crash that cannot be captured Here, your best choice is to create a tag file with a new file (...) deleteOnExit(). Java will clean it up for you if you have a chance

The problem of deadlock is how to detect that you have a deadlock I haven't seen a consistent approach yet

import org.apache.commons.lang.exception.ExceptionUtils;

public class Demo
{
    public static void main (String[] args)
    {
        try
        {
            Demo obj = new Demo ();
            obj.run (args);
            System.out.println ("Done.");
        }
        catch (Throwable t)
        {
            ExceptionUtils.printRootCauseStackTrace (t);
        }
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>