In the exception handling mechanism of Java:
If the exception exception is thrown, there must be a try: Catch.. For processing, belongs to checked exception.
If you throw a RuntimeException exception, you do not have to do a try: Catch.. Exception handling, which occurs after an exception is handled by the JVM and belongs to the unchecked exception.
Note: In order to guarantee the robustness of the program, it is recommended to throw a Runntimeexception exception, also use try: Catch.. Be processed.
The most essential difference between the two is that the designer thinks the user can and should handle the exception.
Categories of Java Exceptions:
The base class is: Throwable
Error and Exception inherit from Throwable
Inheritance of RuntimeException and IOException exception
where error and runtimeexception and their subclasses belong to unchecked exception, while the other exceptions are checked exception.
The error class describes an internal error in the Java runtime and a resource exhaustion scenario where an application should not throw objects of this type (typically thrown by a Java virtual machine). In the event of such an error, there is nothing else to do except to try to get the program safely out of the way. Therefore, in our programming, we should pay more attention to the exception system.
The Runtimeexcption system includes error type conversions, array cross-border access, and attempts to access null pointers and so on. If there is a runtimeexception, then it must be your own fault.
Other non-runtimeexcetpion (IOException, etc.), such exceptions are generally external errors, such as attempting to read data from the end of a file, and so on, which is not an error in the program itself, but an external error that occurs in the application environment.
In Android development, uncheched Exception often leads to crash of the program, in order to provide a good user experience, and to collect the information of the error, in order to improve the program and improve the robustness of the program. Therefore, Thread.uncaughtexceptionhandler is often used for processing.
First you need to inherit the Thread.uncaughtexceptionhandler class
Public classCrashhandlerImplementsThread.uncaughtexceptionhandler { Public Static FinalString TAG = Crashhandler.class. Getsimplename (); Private StaticCrashhandler INSTANCE =NewCrashhandler (); PrivateContext Mcontext; PrivateThread.uncaughtexceptionhandler Mdefaulthandler; PrivateCrashhandler () {} Public StaticCrashhandler getinstance () {returnINSTANCE; } Public voidInit (Context ctx) {Mcontext=CTX; Mdefaulthandler=Thread.getdefaultuncaughtexceptionhandler (); Thread.setdefaultuncaughtexceptionhandler ( This); } @Override Public voiduncaughtexception (thread 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"); NewThread () {@Override Public voidrun () {looper.prepare (); NewAlertdialog.builder (Mcontext). Settitle ("hint"). Setcancelable (false). Setmessage ("The program crashed ..."). Setneutralbutton ("I Know",NewOnclicklistener () {@Override Public voidOnClick (Dialoginterface Dialog,intwhich) {System.exit (0); }}). Create (). Show (); Looper.loop (); }}.start (); } /*** Custom error handling, collect error messages, send error reports, and so on. Developers can customize exception handling logic according to their own situation *@paramex *@returntrue: Returns False if the exception information is processed;*/ Private BooleanHandleException (Throwable ex) {if(ex = =NULL) { return true; } //New Handler (Looper.getmainlooper ()). Post (new Runnable () {//@Override//Public void Run () {//new Alertdialog.builder (Mcontext). Settitle ("hint")//. Setmessage ("program crashes ..."). Setneutralbutton ("I Know", null)//. Create (). Show (); // } // }); return true; }}
Then register in the application class
Public class extends application{ @Override publicvoid onCreate () { super . OnCreate (); Initerrorhandler (); } Private void Initerrorhandler () { = crashhandler.getinstance (); Handler.init (this);} }
This article transferred from: http://blog.csdn.net/wangbole/article/details/8161524
Android Thread.uncaughtexceptionhandler capture