SoFunction
Updated on 2025-04-09

Implementation code for catching global exceptions in Android

1. Implement UncaughtExceptionHandler to handle uncaughtException in the method uncaughtException.

public class GlobalException implements UncaughtExceptionHandler
{  
  private final static GlobalException myCrashHandler = new GlobalException();

  private GlobalException()
  {
  }

  public static synchronized GlobalException getInstance()
  {
    return myCrashHandler;
  }

  public void uncaughtException(Thread arg0, Throwable arg1)
  {
    ("-------------caught Exception--");
  }
}

2. Inherit Application, call the Thread method setDefaultUncaughtExceptionHandler to catch exceptions

Code:

public class MyApplication extends Application 
{
  public void onCreate() 
  {
    ();
    GlobalException handler = ();    
    (handler);   
    
  }
}