What is the Throwable class? The Throwable class is the super class of all errors or exceptions in the Java language. It is a common class that integrates all exceptions. Its function is to extract the error information stored in the stack.
The Error class is a subclass of the Throwable class, including system exceptions, virtual machine exceptions and other problems that cannot be prevented by users.
The Exception class is also a subclass of the Throwable class. It is an exception that can be handled by the program itself. It is divided into runtime exceptions (checked) and non-runtime exceptions (unchecked). Runtime exceptions may or may not be handled; non-runtime exceptions must be handled.
Runtime exceptions are all exceptions of the RuntimeException class and its subclasses, such as NullPointerException, IndexOutOfBoundsException, etc. These exceptions are unchecked and can be captured or not processed in the program. These exceptions are generally caused by program logic errors, and the program should try to avoid the occurrence of such exceptions from a logical perspective.
When a RuntimeException occurs, we do not need to handle it, and the virtual machine usually takes over.
After a runtime exception occurs, if the exception is not caught and processed (that is, there is no catch), the system will throw the exception all the way to the upper layer. If it is multi-threaded, it will be thrown by Thread.run(). If it is single-threaded, it will be thrown by Thread.run(). It is thrown by main(). After throwing, if it is a thread, the thread will exit. If the exception is thrown by the main program, then the entire program will exit. Runtime exceptions are subclasses of the Exception class and also have the characteristics of general exceptions, that is, they can be handled by catch blocks. It's just that we often don't handle it. That is to say, if you don't handle runtime exceptions, then after a runtime exception occurs, either the thread will terminate or the main program will terminate.
If you do not want to terminate, you must catch all runtime exceptions and never let this processing thread exit. If abnormal data appears in the queue, the normal processing should be to discard the abnormal data and then record the log. The existence of abnormal data should not affect the subsequent processing of normal data.
Non-runtime exceptions are exceptions other than RuntimeException, and they all belong to the Exception class and its subclasses. For example: IOException, SQLException, etc. and user-defined Exceptions. For this kind of exception, the Java compiler forces us to catch and handle these exceptions, otherwise the program will not be compiled. Therefore, when facing this kind of exception, whether we like it or not, we have to write a lot of catch blocks to handle possible exceptions.