Java Exception Handling Summary ---------------------------------------------------------------------------------------
1. The language provides a built-in consistent error handling mechanism to avoid inconsistent error handling methods and styles. The basic idea is to let the error source transmit the appropriate information to a receiver for processing. The receiver may be at the same abstraction level as the error source, and more likely at a higher abstraction level. To make a simple analogy, when employees cannot solve certain problems, they must submit them to the senior management for handling.
2. there are not many Exception Handling syntaxes in Java. The key to try-catch-finally, throw, and throws lies in the rational use of exceptions: when to use exceptions; how to use exceptions correctly; when exceptions occur, make sure that the program is in the correct and stable State. You shoshould always ask: When exception happens, will everything properly cleanup?
3. Benefits of the exception handling mechanism: separating the correct case code from error handling.
The code that follows the traditional error handling method is: the code that uses exception handling is:
If1 (! Exception1) {try {
// Correct code 1 // correct code 1 ......
} // Correct case code n
Else1 {} catch (exception1 ){
// Error code 1 // error code 1
}}
...... ......
Interferon (! Exceptionn) {} catch (exceptionn ){
// Correct case code N // error case code n
} Finally {
Elsen {// clear resources or make the program return to a certain State
// Error code n}
}
4. the method for customizing exceptions is simple, so that it inherits from existing exceptions in Java, such as exceptions. an important part of a custom exception is that the name of the exception class must be well obtained, which is descriptive. Because the error source information prompt is very important.
5. An important inheritance system for exceptions: runtimeexception ---> exception ---> throwable, error ---> throwable. Other exceptions are inherited from this system. An important exception method is getclass (). getname () [get exception class name]; getmessage (), getlocalizedmessage () [exception information]; new someexception (string), new someexception (throwable) [constructor]; fillinstacktrace (),
Getcause () [used for re-throwing exceptions]; initcause (throwable) [Exception chain]; printstacktrace (), printstacktrace (printstream), printstacktrace (printwriter) [print stack call information when an exception occurs].
6. java exception chain: Pass the caught exception object as the constructor parameter to the exception object to be thrown, or as a parameter of the initcause () method, the thrown exception can be saved with the information of the caught exception object to form an exception chain, which is used to express the causal relationship.
7. if A1, A2 ,..., an, then the declaration of this method must specify the exception declaration F () throws A1, A2 ,..., an (A1, A2 ,..., if runtimeexception or its derived class is abnormal, you can leave it unspecified .)
8. Exception placeholder or reserved technology: the exception is not thrown in the method, but the exception declaration is still specified in the method. When the exception needs to be thrown in the method, the client code is not affected; because the Customer Code has already handled the exception (before the exception is actually thrown ).
9. Exception declaration restrictions:
(1) The exception declaration list of the subclass constructor must be a superset of the exception declaration list of the base class constructor. That is, if the exception declaration list of the base class constructor is {A1, a2 ,..., an}, then the abnormal declaration list of the subclass constructor s >={ A1, A2 ,... an}; this is because the base class constructor must be called before the subclass constructor is called. Therefore, the subclass constructor must handle all exceptions declared by the base class constructor;
(2) The exception declaration list of the subclass override method must be a subset of the exception declaration list of the base class method. That is, if the exception declaration list of the base class method is {A1, a2 ,..., an}, then the sub-class override method's exception declaration list S <= {A1, A2 ,... an}; this is because the client code is programmed based on the public interface provided by the base class method. It should only handle exceptions declared by the public interface provided by the base class method.
(3) The exception declaration list of sub-interface implementation methods must be a subset of the interface method exception declaration list. This is because the customer code is programmed based on the declaration of the interface method, and only the exception description provided by the interface method should be handled.
10. The special nature of runtimeexception and Its Derived classes:
(1) If an exception occurs, the system will automatically throw a runtimeexception or its derived class object, without explicitly throwing it by the programmer;
(2) If a runtimeexception or its derived class exception is thrown in a method, you do not need to specify these exceptions in the exception description of the method;
(3) Since runtimeexception and Its Derived classes do not need to be explicitly thrown by the programmer, the compiler will not give a try-catch-finally prompt; the programmer may not catch such exceptions, in this way, these runtimeexceptions are escaped from main () through many checkpoints and are not captured. At this time, the program will automatically call the printstacktrace of the exception and terminate the program;
(4) runtimeexception and its derived exceptions usually indicate errors in the program, such as indexoutofboundsexception exceptions in the array. Avoid such exceptions whenever possible.
11. Throwing an exception in the constructor can cause very difficult problems. Therefore, do not throw any exceptions in the constructor as much as possible. Try to make the constructor 100% run successfully.
12. exception matching: When an exception occurs, the system creates an exception object (like creating a common Java object) with new and obtains a reference. Then, the system terminates the current execution path, to the corresponding exception handling program (similar to interrupt processing, but not returned to the original execution path); to match an exception handling program, follow the following rules:
(1) A series of catch statements following the catch statement are usually searched. The search order is based on the order in which the catch statement appears. Once the match succeeds, the catch statements following the catch statement are no longer matched;
(2) The matching principle is: Catch (B) will capture all B and its derived class objects (follow the polymorphism mechanism); otherwise, if A is thrown, all parent class objects of A can be captured. First come first.
(3) exception shielding: If B object is thrown, and B ---> B1 ---> B2 --->... ---> bn, ---> indicates the inheritance relationship. The closer the inheritance level is to B (more precise matching ), the more order the catch statements should be placed in front (if they appear at the same time), that is, the order of catch statement blocks should be followed by catch (B1), catch (B2 ),..., catch (BN ). For example, runtimeexception should be placed before exception, because exception can capture all exceptions, that is, if the order is:... catch (exception
E) {//} catch (runtimeexception e) {}, runtimeexception will not have a chance to execute.
13. Exception usage guidelines: To avoid immediately capturing and handling exceptions, rather than handing them over to appropriate layers for handling. Because the choice of error handling may involve the overall design of the system, rather than local problems. This is similar to the company's Customer Complaint Center: the problem is usually not handled by the receiver of the bottom-layer personnel, but submitted to a more appropriate management layer for handling.
14. check exceptions and troubleshooting methods: Generally speaking, non-runtimeexception exceptions are all checked exceptions. They are checked during compilation and must be processed immediately: or submitted to a higher level, you can either process the catch statement immediately following it. The trouble caused by checked exceptions is that sometimes you do not have enough information to make decisions and handle them (you must submit them to a higher level ), or you don't want to handle it at all (imagine that you call a method of someone, and this method throws an exception, and you will think this does not seem to be your own thing ).
There are two ways to handle the exception: pass to the console, main () throws xxxexception; or wrap it into runtimeexception. When the latter is used, you will not discard the exception (not to be silently swallowed). You do not need to specify the exception description in the method, and the exception will be retained. But somewhere, you still need to process it.
15. Thinking about error handling: what should be the best way to handle errors? In the program, there should be a vast, unexposed Skynet. All errors in the program can be captured and handled in a proper place. Combined with Java Exception Handling Mechanism:. throw an exception where an error may occur to avoid immediate handling (unless there is enough information to know how to handle it); B. when an exception is caught, if you know how to handle it, use the Catch Block for processing; otherwise, you should encapsulate it and throw it again and submit it to a higher level for processing.
16. Thinking in Java: the language provides some good features for programmers to use, but the ultimate right to use is in the hands of programmers. A good language shocould help programmers program well, but no response Ge cocould prevent programmers from bad practice. to learn a language, you should not only understand its syntax and usage, but also understand its design ideas, avoid the shortcomings and traps of language design, and use the excellent features of the language to compile a reliable and maintainable system.