How Android detects unknown exceptions

Source: Internet
Author: User
Tags try catch

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

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.