Android captures global exceptions, while android captures global exceptions.

Source: Internet
Author: User

Android captures global exceptions, while android captures global exceptions.

The Android system "abnormal exit of the program" has a negative impact on the user experience of the application. You can inherit the UncaughtExceptionHandler class to capture exceptions during application running and give friendly prompts. Use the Thread. setDefaultUncaughtExceptionHandler () method to set the exception handling class to the Thread.

1. Exception Handling class, the Code is as follows:

[Java]View plaincopy
  1. Public class CrashHandler implements UncaughtExceptionHandler {
  2. Public static final String TAG = "CrashHandler ";
  3. Private static CrashHandler INSTANCE = new CrashHandler ();
  4. Private Context mContext;
  5. Private Thread. UncaughtExceptionHandler mDefaultHandler;
  6. Private CrashHandler (){
  7. }
  8. Public static CrashHandler getInstance (){
  9. Return INSTANCE;
  10. }
  11. Public void init (Context ctx ){
  12. MContext = ctx;
  13. MDefaultHandler = Thread. getDefaultUncaughtExceptionHandler ();
  14. Thread. setDefaultUncaughtExceptionHandler (this );
  15. }
  16. @ Override
  17. Public void uncaughtException (Thread thread, Throwable ex ){
  18. // If (! HandleException (ex) & mDefaultHandler! = Null ){
  19. // MDefaultHandler. uncaughtException (thread, ex );
  20. //} Else {
  21. // Android. OS. Process. killProcess (android. OS. Process. myPid ());
  22. // System. exit (10 );
  23. //}
  24. System. out. println ("uncaughtException ");
  25. New Thread (){
  26. @ Override
  27. Public void run (){
  28. Logoff. prepare ();
  29. New AlertDialog. Builder (mContext). setTitle ("prompt"). setCancelable (false)
  30. . SetMessage ("the program crashed..."). setNeutralButton ("I know", new OnClickListener (){
  31. @ Override
  32. Public void onClick (DialogInterface dialog, int which ){
  33. System. exit (0 );
  34. }
  35. })
  36. . Create (). show ();
  37. Logoff. loop ();
  38. }
  39. }. Start ();
  40. }
  41. /**
  42. * Custom error handling, collection of error information, and sending of error reports are all completed here. developers can customize the exception handling logic based on their own situations.
  43. *
  44. * @ Param ex
  45. * @ Return true: If the exception information is processed, false is returned.
  46. */
  47. Private boolean handleException (Throwable ex ){
  48. If (ex = null ){
  49. Return true;
  50. }
  51. // New Handler (Looper. getMainLooper (). post (new Runnable (){
  52. // @ Override
  53. // Public void run (){
  54. // New AlertDialog. Builder (mContext). setTitle ("prompt ")
  55. //. SetMessage ("the program crashed..."). setNeutralButton ("I know", null)
  56. //. Create (). show ();
  57. //}
  58. //});
  59. Return true;
  60. }
  61. }

2. Thread binding Exception Handling

[Java]View plaincopy
  1. Public class CrashHandlerActivity extends Activity {
  2. /** Called when the activity is first created .*/
  3. @ Override
  4. Public void onCreate (Bundle savedInstanceState ){
  5. Super. onCreate (savedInstanceState );
  6. SetContentView (R. layout. main );
  7. CrashHandler crashHandler = CrashHandler. getInstance ();
  8. CrashHandler. init (this); // The input parameter must be Activity; otherwise, AlertDialog will not be displayed.
  9. // Creation error
  10. Throw new NullPointerException ();
  11. }
  12. }

Demo: http://code.google.com/p/android-custom-view/downloads/list

Reprinted address:Http://orgcent.com/android-uncaughtexceptionhandler-exception/ | radish cabbage blog



How can android capture global exceptions?

The Android system "abnormal exit of the program" has a negative impact on the user experience of the application. You can inherit the UncaughtExceptionHandler class to capture exceptions during application running and give friendly prompts. Use the Thread. setDefaultUncaughtExceptionHandler () method to set the exception handling class to the Thread.
1. Exception Handling class, the Code is as follows:

[Java] view plaincopy

Public class CrashHandler implements UncaughtExceptionHandler {
Public static final String TAG = "CrashHandler ";
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. setDefaultUncaughtExceptionHandler (this );
}

@ Override
Public void uncaughtException (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 ");

New Thread (){
@ Override
Public void run (){
Logoff. prepare ();
New AlertDialog. Builder (mContext). setTitle ("prompt"). setCancelable (false)
... The remaining full text>
 
Android implements the ThreadUncaughtExceptionHandler interface to capture global exceptions. How can I resolve this exception?

Question added: Write two classes A, B, and an interface C (name self-Fetch) A to catch an Exception and throws Exception {if (arys. length> 4) {throw new Exception ("Array

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.