Understanding of checked and non-checked exceptions
From the beginning, I learned how to handle exceptions. I know that exceptions are classified into checking exceptions and non-checking exceptions. RuntimeException is a non-checking exception and knows the types of exceptions. However, there were not many practical applications before work, and the definitions on the courseware in the books were not very clear about the two exceptions. After work, I gradually learned the basics in the actual code. Here I will record it and I will not talk about it. The courseware on the book is rich in online materials, here we only summarize the understanding of the two exceptions. 1. Check exception: the JAVA compiler forces the use of try {} catch () {} block for processing, if the program is not compiled, an exception must be thrown on the throws display in the method that contains code that may cause an exception. The catch method that calls the method will catch the exception. Otherwise, the program compilation will not pass, there will be a Red Cross prompt. For example:
Here, try catch block must be used to enclose the code or throws explicitly throw an exception, otherwise the compilation will not pass. 2. Non-checkable exceptions: exceptions that can be passed by the JAVA compiler can be handled using try {} catch () {}. After processing, the program can continue to execute downward or not, it is processed by the method called. Of course, it can also be left blank. At the top layer, the thread will be terminated if multiple threads are used, and the main program will be terminated if a single thread is used. That is to say, if a running exception is not processed, it will stop the program. For example:
Although an exception is thrown here, it only prevents the program from continuing to run and does not cause compilation errors. You do not need to explicitly throw an exception using the throws statement in the method.