Java exception capture and handling (1)

1. Abnormal capture and handling
  • KEY WORDS : try, catch, finally, throw, throws.
(1) syntax (code)
try {
 // Code to be run 
} catch (Exception type exception variable name) {
 // Exception handling code 
} finally {
 // Code that needs to be executed before the exception occurs, before the method returns 
}
(2) Characteristics of different sentence blocks
1、try
(1) means trying to run the code, subject to exception monitoring.
(2) When an exception occurs in the code of this statement block, an exception object is thrown. // I don't quite understand what is meant by the thrown object here?
2、catch
(1) When an exception occurs in the code in the try statement block, the catch catches the exception from it and matches it with its own exception type. All the catch statement blocks are matched only once . If it matches, execute the code in the catch and point the catch block parameters to the thrown exception object.
(2) The catch statement takes a parameter of the throwable type, indicating that the type of exception can be caught. // In addition to the exception of 0, the prompt is ArithmeticException. ArithmeticException may be a type of catchable exception.
3. Finally (can be omitted)
(1) There is a catch followed by catch, and no catch followed by try.
(2) In any case, even if no exception occurs in the try statement, this statement block is executed before the method returns. // What does it mean before the method returns?
(3) Generally put statements that release resources and close links into it. (※ 4)
(3) Similarities and differences between different blocks
1. Similarities
The scope of variables in the three code blocks of try, catch, and finally is inside the code block, which are independent and cannot be accessed from each other. If you want to access all three blocks, you need to define variables outside these blocks. (※1)
2. Differences
In an exception handling, there can be one or more try and catch statements, and finally there is only one.
 
2020.04.09 Knowledge of throw and throws is waiting to be added.
This note is an excerpt. Most of the notes are excerpts from other blogs based on my understanding of knowledge.
If improper, please contact the author to delete,
If there are deficiencies, please forgive me and correct me.

Reference link:
※ 1 "Java exception architecture" 
※ 2 "Use of try and catch in Java"
※ 3 "Using finally in Java"
※4
 
 
 

Guess you like

Origin www.cnblogs.com/wongman/p/12670865.html