Android thread. uncaughtexceptionhandler capture

Source: Internet
Author: User

In Java's Exception Handling Mechanism:
If an exception is thrown, try... catch... must be included for processing. This is a checked exception.
If a runtimeexception is thrown, it is not necessary to handle try... catch... exceptions. When an exception occurs, it will be handled by JVM, which is an unchecked exception.
Note: To ensure program robustness, we recommend that you throw a runntimeexception and use try... catch... to handle it.

The essential difference between the two lies in whether the designer determines whether the user can and should handle this exception.

Java exception classification:
Base Class: throwable
Error and exception are inherited from throwable
Runtimeexception and ioexception inherit exception
Among them, error, runtimeexception and its subclass belong to unchecked exception, while other exceptions are checked exception.

The error class describes internal errors in the Java running system and resource depletion. Applications should not throw such types of objects (usually thrown by the Java Virtual Machine ). In addition to trying to secure the exit of the program, this error is not feasible in other aspects. Therefore, we should pay more attention to the exception system during program design.

Runtimeexcption system, including incorrect type conversion, array out-of-bounds access, and attempts to access null pointers. If runtimeexception occurs, it must be your own error.

Other non-runtimeexcetpion (ioexception, etc.), such exceptions are generally external errors, such as attempting to read data from the end of the file. This is not a program error, an external error occurs in the application environment.

In Android development, the program crash is often caused by uncheched exception. In order to provide a good user experience and collect error information, the program can be improved, improve program robustness. Therefore, thread. uncaughtexceptionhandler is often used for processing.

First, you must inherit the thread. uncaughtexceptionhandler class

Public class crashhandler implements thread. uncaughtexceptionhandler {public static final string tag = crashhandler. class. getsimplename (); Private Static crashhandler instance = new crashhandler (); private context mcontext; private thread. uncaughtexceptionhandler mdefaulthandler; private crashhandler () {} public static crashhandler getinstance () {return instance;} public void Init (context CTX) {Mcontext = CTX; mdefaulthandler = thread. getdefaultuncaughtexceptionhandler (); thread. Handle (this) ;}@ override public void uncaughtexception (thread, throwable ex) {// If (! Handleexception (Ex) & mdefaulthandler! = NULL) {// mdefaulthandler. uncaughtexception (thread, ex); //} else {// android. OS. process. killprocess (Android. OS. process. mypid (); // system. exit (10); //} system. out. println ("uncaughtexception"); New thread () {@ override public void run () {loid. prepare (); New alertdialog. builder (mcontext ). settitle ("prompt "). setcancelable (false ). setmessage ("the program crashed... "). setneutralbutton ("I know", new onclicklistener () {@ override public void onclick (dialoginterface dialog, int which) {system. exit (0 );}}). create (). show (); logoff. loop ();}}. start ();}/*** custom error handling, collecting error messages, sending error reports, and other operations are completed here. developers can customize the exception handling logic ** @ Param ex * @ return true: If the exception information is processed, false */private Boolean handleexception (throwable ex) is returned) {If (EX = NULL) {return true;} // new handler (loler. getmainlooper ()). post (New runnable () {// @ override // public void run () {// new alertdialog. builder (mcontext ). settitle ("prompt ")//. setmessage ("the program crashed... "). setneutralbutton ("I know", null )//. create (). show (); //}); Return true ;}}

Then register in the application class

public class MyApplication extends Application{    @Override    public void onCreate(){super.onCreate();        initErrorHandler();    }   private void initErrorHandler(){CrashHandler handler = CrashHandler.getInstance();handler.init(this);   }}

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.