Android uses Uncaughtexceptionhandler to catch global exceptions

Source: Internet
Author: User

Resources:

Http://blog.csdn.net/hehe9737/article/details/7662123

Http://www.apkbus.com/android-128270-1-1.html

Http://www.cnblogs.com/freeliver54/archive/2011/10/17/2215423.html

Thread.setdefaultuncaughtexceptionhandler (New Uncaughtexceptionhandler () {//) sets a handler for the main thread to handle run-time exceptions

public void uncaughtexception (thread thread, final Throwable ex) {

Ex.printstacktrace ();

When the program appears crash, it will enter here, you can do some operations here, and then put System.exit (0);

//

}

});

public static void Setdefaultuncaughtexceptionhandler (Thread.uncaughtexceptionhandler eh) sets when a thread terminates abruptly because of an uncaught exception. And there is no default handler that is called when other handlers are defined for the thread.

Uncaught exception handling is first controlled by the thread, then controlled by the thread's Threadgroup object, and finally by the uncaught default exception handler. If the thread does not set an explicit uncaught exception handler, and the thread Group of the thread (including the parent thread group) does not specifically specify its Uncaughtexception method, the default handler's Uncaughtexception method is called.

By setting the default exception handlers that are not caught, the application can change the uncaught exception handling (such as logging to a specific device or file) for threads that have accepted any "default" behavior provided by the system.

Note that the default exception handler that is not caught should generally not conform to the thread's Threadgroup object, because this can lead to infinite recursion.


Parameters:
Eh-the object that is used as the default exception handler that is not caught. If NULL, there is no default handler.

static void Setdefaultuncaughtexceptionhandler (Thread.uncaughtexceptionhandler eh)

Set a default handler for a thread that is called when the program cannot be captured and exits, and if there are other handler

So as long as the set method, a handler for a thread can be in the program to exit the exception, the catch exception,

Http://www.eoeandroid.com/thread-167271-1-1.html

In JDK5.0, we can set a uncaughtexceptionhandler for any thread by Setuncaughtexceptionhandler the instance method of thread.

Of course you can also set a default uncaughtexceptionhandler for all thread, by calling Thread.setdefaultuncaughtexceptionhandler ( Thread.uncaughtexceptionhandler eh) method, which is a static method of thread.

Android uses Uncaughtexceptionhandler to catch global exceptions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.