Java exception handling learning notes
If you have already understood an exception, you don't have to look. This is written to beginners.
Basic knowledge
Some abnormal phenomena often occur when running, this situation is called running errors. Depending on its nature, it can be divided into errors and abnormalities.
Error: Common programs enter the dead cycle, memory leaks, etc. In this case, the program is still unable to solve it, and can only intervene by other methods. The corresponding class is ERROR class
Abnormal: Common has a division of 0, an array of beys, and the like. In this case, the program is not resolved when the program is running, and the operation direction is running by the exception code adjustment, so that the program can continue to run until the end. The corresponding class is an Exception class.
Throw an exception: When the program is abnormal, an exception event is generated, generating an exception object, and submits it to the running system, and then finds the corresponding code to handle the abnormality, this process is called throwing exception.
Capture an exception: After the abnormality throws, the runtime system starts from the generated object, and the invoking plan of the method is traced back, until the method containing the corresponding processing, and hand it over to the method, this process is called Capture exception.
Five major templates
Capture and processing exceptions using try-catch-finally statement
Figure one five major templates
A little explanation to the template:
1. TRY statement When a certain code may generate an exception at runtime, this code should be placed in the TRY statement. But don't put all the statements in the TRY statement because the particle size is too large.
2. The parameter in the CATCH statement is similar to the declaration of the method, contains an exception type and an exception object. The exception type must be a subclass of the Throwable class, and of course it can be a self-defined anomaly class.
A. A TRY sentence can have multiple catch statements below, handling different exceptions separately. However, the order of arrangement must be from special to general, and the last one is generally an Exception class (Template 4).
B. You can also handle multiple exception types with a catch statement. At this time it's an exception type parameter should be the superclass of these exception types (template 3).
C. Java runtime system detects the exception type of each CATCH statement from top to bottom, until the Catch statement matched with the type is found. Matching refers to the exception type processed by Catch and the generated exception type or its superclass.
D. If the exception generated by the program does not match all CATCH processing, this exception will be captured and processed by the Java virtual machine. At this time, it is the same as not using isoform.
3. Finally statement is in the code defined by TRY, when an exception is thrown, the subsequent code will not be executed. You can specify a piece of code through a Finally statement. Whether you throw or do not throw or do not throw an exception in the program block specified by TRY, whether the code specified by the CATCH statement is consistent with the type of exception thrown, it provides a unified exit. . This statement is also omitted (template 2).
4. Abnormal statement nested When the abnormality is dealing with an exception, this phenomenon is very common. Common forms are template five.
Common abnormal class
Everyone maybe in the process of programming, I will realize that there is an error here, but I don't know what abnormality will be thrown, and it is a headache. This part will be summarized as follows:
Math exception class: ArithmeticExecption
Empty pointer anomaly class: nullpointersRexception
Type forced conversion exception: ClassCastException
The number of negative subscripts is abnormal: NegativeArrayException
Array Underline Experience: ArrayIndexOfboundsexception violates security principles: SECTURITYEXCEPTION
The file has ended: EOFException
The file is not found: filenotfoundexception
String conversion to digital exception: NumberFormATexception
Operating database exception: SQLException
Input and output exception: ioException
Method did not find an exception: Nosuchmethodexception
This is the most common exception. You can summarize in the future, but the most detailed probably still JDK documentation. Still watching the JDK documentation :);
Exception method
Public Class Exception Extends throwable
Construction method: public exception ()
Public Exception (String S)
Example: public string getMessage ()
Public void printstacktrace ()
Differences and contacts from throw and throws
Difference 1: throw is a statement throw an exception; throws is how an exception is thrown;
Throw Syntax: Throw
In the method declaration, the THROWS clause indicates that the method will throw an exception.
THROWS Syntax: [
Among them: The abnormal class can declare multiple, divided by comma.
Difference 2: ThROWS can be used alone, but throw cannot be;
Difference 3: THROW Either uses the try-catch-finally statement to use it, or use with the throws. But ThROWS can be used alone and then captured by methods of processing exceptions.
Customized exception class
Like a category that is declared, it must be inherited in an Exception class. This custom class can be thrown with throw and throws.