• Runtime exception. Is RuntimeException class and its subclasses abnormalities, abnormal are tested, such as system exception or a program logic, we often encounter a NullPointerException, IndexOutOfBoundsException, etc. Upon such an exception, the Java Runtime will stop the thread, print an exception, and the program will stop running, often referred to as a crash.

  • Non-runtime exception. Exceptions are exceptions that belong to the Exception class and its subclasses. Exceptions that are checked are exceptions other than RuntimeException. Exceptions such as NoSuchFieldException and IllegalAccessException must be handled in the program, without which the program will not compile properly.

Abnormal classification

  1. Runtime exception. Is RuntimeException class and its subclasses abnormalities, abnormal are tested, such as system exception or a program logic, we often encounter a NullPointerException, IndexOutOfBoundsException, etc. The Java Runtime will stop the thread, print exceptions, and it will stop the program and crash.

  2. Non-runtime exception. Exceptions are exceptions that belong to the Exception class and its subclasses. Exceptions that are checked are exceptions other than RuntimeException. This type of exception must be handled in the program. If not handled, the program cannot be compiled properly. For example, NoSuchFieldException will be checked during compilation

The principle of

The zygote thread sets an UncaughtHandler() during initialization. This method handles exception logic by catching exceptions when they occur on child and main threads. KillApplicationHandler() The system default exception handler that handles child threads and the main thread crashes

Schematic diagram of child threads

The handle mechanism is also the logic of continuous rotation of the main thread, when the main thread is abnormal. Catch the main thread with a try catch,

If an exception is thrown in the activity declaration cycle method, the life cycle will be incomplete and there will be other exceptions

// Define CrashHandler to handle child thread Carsh

class CrashHandler : Thread.UncaughtExceptionHandler {

private var context: Context? = null

fun init(context: Context?) {

this.context = context

Thread.setDefaultUncaughtExceptionHandler(this)

}

override fun uncaughtException(t: Thread, e: Throwable) {}

companion object {

val instance: CrashHandler by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {

CrashHandler() }

}

}

// Application’s onCreate() method is initialized

CrashHandler. The instance. The init (this) / / main thread carsh processing Handler (stars) getMainLooper ()). The post {the while (true) {try {stars. The loop () } catch (e: Throwable) { } } }

// When you click the button in the activity, the main thread and child thread handwriting exceptions do not crash.

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn).setOnClickListener( view -> { new Thread(new Runnable() { @Override public void run() { int a=1/0; } }).start(); }); }