To catch exceptions that are not captured using try catch, you can use the Java interface uncaughtexceptionhandler to implement the default exception capture mechanism. When an unknown exception occurs during program execution, you can implement uncaughtexceptionhandler to capture exceptions.
Usage:
1. Implement the uncaughtexceptionhandler interface first
After implementing the uncaughtexceptionhandler interface, rewrite the uncaughtexception method. When an unknown exception occurs, this method is executed.
Note that you need to call the uncaughtexceptionhandler's uncaughtexception method of the parent interface. Otherwise, an ANR will occur, and the pop-up will not pop up. [Sorry, "XXX" has stopped running .] Dialog box.
Public class customexceptionhandler implements uncaughtexceptionhandler {Private Static final string tag = "customexceptionhandler"; private uncaughtexceptionhandler mdefaultueh; Public customexceptionhandler () {log. D (TAG, "------------ customexceptionhandler ----------"); mdefaultueh = thread. getdefaultuncaughtexceptionhandler () ;}@ overridepublic void uncaughtexception (thread, throwable ex) {log. E (TAG, "------------ uncaughtexception ------------" + ex. getmessage (); mdefaultueh. uncaughtexception (thread, ex); // This statement will cause ANR }}
2. Set the customexceptionhandler class to the default exception capture class in the activity class.
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Thread.setDefaultUncaughtExceptionHandler(new CustomExceptionHandler()); int a = 1 / 0; }
When an exception occurs when the divisor is 0 in 1/0, it will be captured by the uncaughtexception method of the customexceptionhandler class, that is, this method is executed. You can perform corresponding processing, such as releasing resources, save settings.
3. Run the program. The log output is as follows:
08-14 15:21:45. 265: D/customexceptionhandler (13482): ------------ customexceptionhandler ------------
08-14 15:21:45. 265: D/androidruntime (13482): Shutting Down VM
08-14 15:21:45. 265: W/dalvikvm (13482): threadid = 1: thread exiting with uncaught exception (group = 0x40fbc300)
15:21:45-14. 265: E/customexceptionhandler (13482): ------------ uncaughtexception ---------- unable to start activity componentinfo {COM. test. andtest/COM. test. andtest. mainactivity}: Java. lang. arithmeticexception: divide by zero
08-14 15:21:45. 265: E/androidruntime (13482): Fatal exception: Main
15:21:45-14. 265: E/androidruntime (13482): Java. lang. runtimeexception: unable to start activity componentinfo {COM. test. andtest/COM. test. andtest. mainactivity}: Java. lang. arithmeticexception: divide by zero
08-14 15:21:45. 265: E/androidruntime (13482): at Android. App. activitythread. initialize mlaunchactivity (activitythread. Java: 2110)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. App. activitythread. handlelaunchactivity (activitythread. Java: 2135)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. App. activitythread. Access $700 (activitythread. Java: 131)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. App. activitythread $ H. handlemessage (activitythread. Java: 1228)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. OS. handler. dispatchmessage (handler. Java: 99)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. OS. Looper. Loop (Looper. Java: 137)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. App. activitythread. Main (activitythread. Java: 4866)
08-14 15:21:45. 265: E/androidruntime (13482): At java. Lang. Reflect. method. invokenative (native method)
08-14 15:21:45. 265: E/androidruntime (13482): At java. Lang. Reflect. method. Invoke (method. Java: 511)
08-14 15:21:45. 265: E/androidruntime (13482): At com. Android. Internal. OS. zygoteinit $ methodandargscaller. Run (zygoteinit. Java: 786)
08-14 15:21:45. 265: E/androidruntime (13482): At com. Android. Internal. OS. zygoteinit. Main (zygoteinit. Java: 553)
08-14 15:21:45. 265: E/androidruntime (13482): At Dalvik. system. nativestart. Main (native method)
08-14 15:21:45. 265: E/androidruntime (13482): caused by: Java. Lang. arithmeticexception: divide by zero
08-14 15:21:45. 265: E/androidruntime (13482): At com. Test. andtest. mainactivity. oncreate (mainactivity. Java: 15)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. App. activity. Wait mcreate (activity. Java: 5143)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. App. instrumentation. callactivityoncreate (instrumentation. Java: 1079)
08-14 15:21:45. 265: E/androidruntime (13482): at Android. App. activitythread. initialize mlaunchactivity (activitythread. Java: 2074)
08-14 15:21:45. 265: E/androidruntime (13482):... 11 more