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
- 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)
- . SetMessage ("the program crashed..."). setNeutralButton ("I know", new OnClickListener (){
- @ Override
- Public void onClick (DialogInterface dialog, int which ){
- System. exit (0 );
- }
- })
- . Create (). show ();
- Logoff. loop ();
- }
- }. Start ();
- }
- /**
- * 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.
- *
- * @ Param ex
- * @ Return true: If the exception information is processed, false is returned.
- */
- Private boolean handleException (Throwable ex ){
- If (ex = null ){
- Return true;
- }
- // New Handler (Looper. getMainLooper (). post (new Runnable (){
- // @ Override
- // Public void run (){
- // New AlertDialog. Builder (mContext). setTitle ("prompt ")
- //. SetMessage ("the program crashed..."). setNeutralButton ("I know", null)
- //. Create (). show ();
- //}
- //});
- Return true;
- }
- }
2. Thread binding Exception Handling
[Java]View plaincopy
- Public class CrashHandlerActivity extends Activity {
- /** Called when the activity is first created .*/
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. main );
- CrashHandler crashHandler = CrashHandler. getInstance ();
- CrashHandler. init (this); // The input parameter must be Activity; otherwise, AlertDialog will not be displayed.
- // Creation error
- Throw new NullPointerException ();
- }
- }
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