abnormal

It often refers to abnormal phenomena that occur during the running of the program, such as user input errors, division by zero, the file to be processed does not exist, and the array subscript is out of bounds, etc.

In Java's exception handling mechanism, a lot of classes are introduced to describe and handle exceptions, called exception classes. The exception class definition contains the information of the exception and the method for handling the exception.

Java exception class hierarchy
picture

Error is an error that cannot be handled by the program, indicating a more serious problem in running the application.

Exception is an exception that the program itself can handle. The Exception class is the parent class of all exception classes, and its subclasses correspond to a variety of possible exception events. Usually Java exceptions can be divided into:

  • RuntimeException runtime exception

Exceptions derived from RuntimeException, such as division by 0, out-of-bounds array subscript, null pointer, etc., are generated frequently and are troublesome to handle. Such exceptions are usually caused by programming errors, so when writing programs, it is not required to use Exception handling mechanisms to handle such exceptions often need to add "logic handling to avoid these exceptions".

  • CheckedException checked exception

Checked Exception, also known as "checked exception", such as IOException, SQLException, etc. and user-defined Exception exceptions. Such exceptions must be handled at compile time, otherwise the compilation will fail.

Process :

  1. Throwing an exception: When executing a method, if an exception occurs, the method generates an object representing the exception, stops the current execution path, and submits the exception object to the JRE.
    Note: The principle of declaring exceptions in method overriding: When a subclass overrides the superclass method, if the superclass method declares an exception, the scope of the exception declared by the subclass cannot exceed the scope declared by the superclass.

  2. Catch the exception: After the JRE gets the exception, it looks for the corresponding code to handle the exception. The JRE looks in the method's call stack, backtracking from the method that generated the exception until it finds the corresponding exception-handling code.

Catching exceptions is achieved through three keywords: try-catch-finally. Use try to execute a program. If an exception occurs, the system throws an exception, which can be caught and processed by its type. The last step is to provide a unified exit for exception handling through the finally statement, which is specified by finally. The code must be executed (there can be multiple catch statements; there can only be one finally statement at most, which can be optional according to your own needs).

Catch order when catching exceptions: If there is an inheritance relationship between exception classes, pay attention to the sequence arrangement. The more the top-level class is, the lower it is, otherwise the redundant catch will be omitted directly. That is, first catch subclass exceptions and then catch parent class exceptions.

Note:

  1. The finally statement executes even if there is a return statement in the try and catch blocks. It is to exit through return after executing the finally statement.

  2. There is only one case in which the finally block will not be executed, that is, it encounters System.exit(0) before executing finally to end the program.

  3. Exception handling can also throw exceptions manually

try catch finally return execution sequence

  1. Execute try, catch, assign a value to the return value
  2. execute finally
  3. return

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325082569&siteId=291194637