“This is the 17th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”.

The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

Note: yesterday we looked at all the exception handling mechanisms, so let’s summarize what we learned about exceptions and extreme mechanisms!

👺 What is the exception?

An abnormal condition in the execution of a program that will eventually cause the JVM to stop abnormally. In object-oriented programming languages such as Java, an exception is itself a class, and to raise an exception is to create and throw an exception object.

👣 The architecture contained in the exception

Abnormal system in a picture:

  • We can see the overall inheritance relationship of exception classes. Of course, not all exceptions are listed in the figure above, and many subclasses are not listed. Here are the more commonly used exception classes. Of course, users can also customize their own exception implementations.

The Throwable contains:

  • Error: An Error that cannot be handled in the program. It indicates that a serious Error occurred in running the application. Most errors have nothing to do with what the code writer is doing, and instead represent problems with the JVM while the code is running.
  • Exception: An Exception that can be caught and handled by the program itself. When such exceptions occur, we need to correct the code and fix the program. Exceptions fall into two categories: runtime and compile-time

😮 Exception handling mechanism

In Java applications, the exception handling mechanism is: throw an exception, catch an exception.

  • Throw: Throws an exception of type Throwable inside a method. Any Java code can throw an exception through a throw statement.

  • Throws: The throws statement is used when a method is defined to declare the type of Exception to be thrown by the method. If the Exception type is thrown, the method is declared to throw all exceptions.

  • Try, finally, catch

    1. Try block: Used to catch exceptions. This may be followed by zero or more catch blocks, or if there is no catch block, a finally block must follow.

    2. Catch block: Used to handle exceptions caught by a try.

    3. Finally block: Statements ina finally block are executed whether or not exceptions are caught or handled. When a return statement is encountered ina try or catch block, the finally block is executed before the method returns. A finally block is not executed in four special cases:

      • An exception occurred in the finally statement block.
      • I used it in the previous codeSystem.exit()Exit the program.
      • The thread on which the program resides dies.
      • Close the CPU.

So how do we choose the exception type?

You can use the following figure to choose whether to catch, declare, or throw an exception

The rules we should follow in everyday exception handling code are:

  • Don’t catch exceptions like Exception, catch specific exceptions like Exception, it’s easy to troubleshoot problems, and it’s less likely that other people will scold you when they take over your code.
  • Don’t eat anything raw. This is something to pay special attention to in exception handling. If we don’t throw the exception, or if we don’t log it, the program may end up in an uncontrollable fashion later on. Sometimes you need to debug code online.

🌸 summary

Now that you are familiar with the concept of exception handling, we will learn about the new exception features in JDK1.7. Look forward to more in the next chapter. Welcome to the next chapter!

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!

\