SoFunction
Updated on 2025-03-11

Android exception handling best practices

A good app exception handling mechanism I think it should include at least the following functions:

1. Can upload error information to the server, allowing developers to continuously improve the app

2. The error message should at least contain information that can help programmers to locate whether it is in the main process or whether it is in the main thread.

3. It is best to include mobile phone hardware and software information.

4. The exceptions caused by the main process are best left to the system to handle themselves, which means that users can perceive that (of course, you can also define a set of more interesting perception system dialog boxes, etc., for details, please refer to various interesting 404 interfaces)

5. It is best not to let the user perceive the exception caused by the child process. For example, push and other things are related to user perceptions. It is best to kill it directly if an exception occurs. Don't leave it to the system for processing anymore.

The following code is listed below.

package ;

import ;
import ;
import ;

/**
 * Created by Administrator on 2015/12/9.
 */
public class BaseApplication extends Application {

  public static Context mContext;
  //Default exception handling  public static  defaultUncaughtExceptionHandler;

  @Override
  public void onCreate() {
    ();
    mContext = this;
    //Get the default exception handling handler first    defaultUncaughtExceptionHandler = ();
    (new BaseUncaughtExceptionHandler());
  }


}
package ;

import ;
import ;
import ;

/**
 * Created by Administrator on 2015/12/9.
 */
public class Utils {

  /**
    * Determine whether it is executed in the main thread. If it is returned true, it does not return false.
    */
  public static boolean isInMainThread() {
    //Note this point, we cannot definitely get the value of myLooper. For example, your thread does not have a bound message loop.    //Then your mylooper will return null, and the corresponding value will be returned only after binding    return () == ();
  }


  //Judge whether it is the main process. If yes, return true. Otherwise, return false  public static boolean isMainProcess(Context context)
  {
    return ().equals(getProcessName(context));
  }

  //Get the process name  public static String getProcessName(Context context) {
    String currentProcessName = "";
    int pid = ();
    ActivityManager manager = (ActivityManager) (Context.ACTIVITY_SERVICE);
    for ( processInfo : ()) {
      if ( == pid) {
        currentProcessName = ;
        break;
      }
    }
    return currentProcessName;
  }

}

package ;

import ;
import ;
import ;

/**
 * Created by Administrator on 2015/12/9.
 */
public class BaseUncaughtExceptionHandler implements  {

  @Override
  public void uncaughtException(Thread thread, Throwable ex) {
    Writer resultWriter = new StringWriter();
    PrintWriter printWriter = new PrintWriter(resultWriter);
    (printWriter);
    StringBuffer sb = new StringBuffer();
    ("Whether an exception occurred in the main thread" + () + "\n");
    ("Whether an exception occurred in the main process" + () + "\n");
    String errorReport = () + ();
    //This place is best to upload the collected error information log to the server to facilitate developers to locate and modify problems.    //If an exception occurs in the main process, it will be left to the system itself to handle the default exception.  Let the user perceive it, otherwise the user will know nothing. The experience is not good    //Of course you can also define special error prompts yourself, such as some interesting dialogs or something    if (()) {
      (thread, ex);
    } else {
      //If an exception occurs in a child process, don't give a prompt. It is best to kill the child process directly. Don't let the user perceive it.      (());
    }
  }


}

The above is all about this article, I hope it will be helpful for everyone to learn Android software programming.