Handling and Capture of Exceptions in Java

Handling and Capture of Exceptions in Java

1. An overview of exceptions

(1) The role of exceptions in Java: enhance the robustness of the program
(2) All Errors and Exceptions in Java inherit the same parent class Throwable
insert image description here

2. The cause of the exception (type of Java exception)

There are three main reasons for exceptions in Java:
(1) Exceptions caused by internal program errors . For example, ArrayIndexOutOfBoundsException, NullPointerException, etc. This kind of exception is called unchecked exception . This kind of exception does not need to be declared and thrown in the code, but it needs to be handled in the class.
All kinds of exceptions under RuntimeException are unchecked exceptions .
(2) An exception manually generated by a throw (throwing an exception) statement is called a checked exception . Checked exceptions can be detected at compile time, and must be handled in the code or declared to be thrown, otherwise the compilation will fail. For example: IOException, FileNotFoundException, ClassNotFoundException, SQLException, etc.
(3) Error : This type of exception is usually caused by the Java virtual machine (jvm) or hardware. For example: OutOfMemoryError, StackOverFlowError, etc.

3. Exception handling

3.1 Exception throwing (throw)

1. Throw is to throw an exception to the caller, and the caller will process the exception information. When writing a program, if an error occurs in the program, it is necessary to notify the caller of the exception information. Here, the keyword throw can be used to throw a specified exception object and notify the caller of the error information.
2. Throw must be written in the method body, and once an exception occurs, the code behind it will not be executed.
insert image description here
insert image description here

3. If a compile-time exception (checked exception) is thrown, we must deal with this exception, otherwise the compilation will not pass.
insert image description here
insert image description here

insert image description here

If a runtime exception (unchecked exception) is thrown, it can be directly handed over to the jvm for processing without processing. (Actually, it’s best to deal with it all at once if it can be dealt with!!!)
insert image description here
insert image description here

3.2 Abnormal capture

3.2.1 throws capture exception

Throws is after the method declaration parameter list. When the method throws a compile-time exception and you don't want to handle the exception, you can use throws to throw the exception to the caller of the method for processing.
1. Format:
modifier return value type method name (parameter list) throws exception type { }

insert image description here

2. If multiple exceptions are thrown in the method, throws must be followed by multiple exception types, separated by commas
insert image description here

3. If multiple exceptions are thrown and there is a parent-child relationship, directly declare the parent class
insert image description here

4. When calling a method that declares to throw an exception, the caller must handle the exception, or continue to use throws to throw.
insert image description here

3.2.2, try-catch captures and handles exceptions

1. When the program throws an exception, we can capture and process the exception through try-catch.
insert image description here
insert image description here

2. It needs to be clear that if the exception thrown by the program is not handled, it will be handed over to the jvm for processing, and the jvm processing will immediately terminate the program. In this way, we can understand that when using try-catch for exception capture, the corresponding exception must be caught. If the corresponding exception is not caught, it will also be processed by jvm.
insert image description here
insert image description here

4. If try throws multiple exceptions, it must be captured with multiple catches. Note here that capturing with multiple catches is not simultaneous capture, because different exceptions cannot be thrown at the same time.
insert image description here

It can also be abbreviated as follows:
insert image description here
insert image description here

5. If there is a parent-child relationship between exceptions, then the subclass exception must come first, and the parent class exception must be caught after, otherwise an error will be reported.
insert image description here

3.2.3、finally

1. Finally, regardless of whether the program is throwing an exception or not, finally will be executed.
2. finally is used for resource recovery or some finishing operations, regardless of whether the program is running normally or exiting, resources need to be recovered.
Case 1: When an exception is thrown, finally will be executed.
insert image description here
insert image description here

Case 2: After the program segment ends normally, the statement under finally will also be executed
insert image description here
insert image description here

3. Even if there is return, finally will be executed.
insert image description here
insert image description here

4. Summary:
throw throws an exception, throws declares and catches the exception, and try-catch catches the exception
finally statement will definitely execute

3.3 Custom exception class

Although there are many exception classes in Java, we will encounter many exceptions during development, which cannot be fully represented, which requires us to solve them through custom exception classes.
Above code:
first: custom exception class MyException
insert image description here

Throw a custom exception class and capture it
insert image description here
insert image description here

Summary of custom exception classes:
Custom exception classes inherit Exception or RuntimeException by default.
Exceptions inherited from Exception are checked exceptions by default.
Exceptions inherited from RuntimeException are unchecked exceptions by default.

insert image description here
insert image description here

insert image description here

Note that when dealing with inherited Exception (checked) custom exceptions, the thrown exception must be captured, otherwise the compilation will not pass.

Guess you like

Origin blog.csdn.net/weixin_43950588/article/details/130798493