Android Global catch exception handling

Source: Internet
Author: User

Many times when we run the program, once an exception occurs, and we do not capture in the program, no processing, will pop up a

dialog box, causing the program to collapse, the user experience of this situation is very poor, and then how to solve the problem, the program any missing exception handling we can

Caught, is the global exception capture, the code is as follows:

1  PackageCom.qdsj.hxcmjj.util;2 3 ImportJava.lang.Thread.UncaughtExceptionHandler;4 5 ImportAndroid.app.ProgressDialog;6 ImportAndroid.content.Context;7 8 /**9 * Exception Handling classTen  * @authorJansin One  * A  */ -  Public classCrashhandlerImplementsUncaughtexceptionhandler { -      Public Static FinalString TAG = "Crashhandler"; the     Private StaticCrashhandler INSTANCE =NewCrashhandler (); -     PrivateContext Mcontext; - ProgressDialog Loadingdialog; -  +     PrivateCrashhandler () { -     } +  A      Public StaticCrashhandler getinstance () { at         returnINSTANCE; -     } -  -      Public voidInit (Context ctx) { -Mcontext =CTX; -Thread.setdefaultuncaughtexceptionhandler ( This); in          -         //initializing exception handling classes to     } +  - @Override the      Public voiduncaughtexception (Thread arg0, Throwable arg1) { *      $         //after catching the exception, do the processing here.Panax Notoginseng     } -  the  +}
Uncaughtexceptionhandler Global exception capture class as shown in the code

With this custom class, we also need to initialize the exception-handling class before the program runs, preferably in the application class, to initialize the global exception-capturing class.
With this class, we can solve the problem that our missed exception capture causes the program to crash.

Android Global catch exception handling

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.