When writing Java programs, exceptions are usually prompted to be caught, and some exceptions do not need to be caught forcibly. This is a bad topic. People who have been transferred from other languages like me are indeed a little confused, so I will reinterpret it in my understanding.
The base class of exception is Exception, and the Exception subclasses include RuntimeException and other Exceptions. These other Exceptions are called Checked exceptions, and RuntimeException is called Unchecked exceptions.
It is not easy to understand just by looking at the name. To put it simply, Java prompts the developer to catch known exceptions in order to enable the program to run stably. The compiler knows that all types or methods may throw exceptions, and when you use a certain type or method, the compiler will prompt you to catch known exceptions. The possible exception known to these compilers is the Checked exception. For example, when you close the file stream, the IOException has already stated in the close method that it may be thrown, and the compiler prompts you that the exception must be caught. The RuntimeException exception is not known during the compilation stage, and can only be determined during the running stage. For example, 3/0 (3 divided by 0) will report an ArithmeticException exception. Because this divisor can be changed in the running stage, no prompt for capture. These RuntimeExceptions are Unchecked exceptions.
In short, Java is to make the program stable as much as possible. If you know, you will be prompted, and if you don’t know, you will be powerless. This explanation should be clearer.
Let's get to the point.
Some friends may have encountered this situation when debugging the program. The program clearly had an exception and caught (Exception e), but no information was captured. There are only two reasons: 1. The thread where the exception is located is not the same thread as the thread you are catching, 2. The program throws not an Exception but an Error. Error, like Exception, inherits from Throwable, refers to a serious error that should not be caught. When I saw this explanation, I was so stupid that I didn't understand why I shouldn't catch the Error. Because an error occurs, the program will not run directly, so it is meaningless to catch it. Then my problem comes again. If it is not captured, the program will exit if there is a problem and cannot even see the log. What should I do? In fact, this assumption is not true, because if Error really exists, you will have discovered problems in the development environment and it is impossible to publish them to the official environment.
Alas, I went around a long way and did such a stupid thing, so don’t discuss whether Error should be captured!
I am still knowledgeable and the purpose of writing is to get everyone's guidance. If the article helps you, it would be great.