Java unusual learning experience This article has some concept of abnormal mechanism in Java. The purpose of writing this article is that I have forgotten these things for a long time. This article can be quickly recalled. 1. Abnormal mechanism 1.1 The anomaly mechanism is how the program handles the program after the program has an error. Specifically, the abnormal mechanism provides a secure channel that exits the program. When an error occurs, the process of program execution changes, and the control of the program is transferred to an exception processor. 1.2 Traditional processing exceptions is that the function returns a special result to indicate an exception (usually this special result is a conventionally known), calling the function to check and analyze the results returned by the function. This has the following draw: For example, the function returns -1 represents an exception, but if the function does return to -1, it will be confused when the correct value is to return; the readability is reduced, and the program code is mixed with the code. To analyze errors by the program that call the function, this requires the customer programmer to have a deep understanding of the library function. 1.3 Abnormal Process 1.3.1 encounter an error, the method ends immediately, does not return a value; at the same time, throw an exception object 1.3.2 Calling the program does not continue to execute, but search for a handleable This exception's abnormal processor, and executes the code 2 exception classification 2.1 Exceptions of unusually classification 2.1.1 Exception, the base class for throwable, error and exception inherited through Throwable, RuntimeException and IOException, etc. Inherit Exception, specific runtimeException inheritance RuntimeException. 2.1.2 Error and RuntimeException and their subclasses become unchecked, other abnormalities become checked. 2.2 Each type of exception 2.2.1 Error System Error class system describes the internal error in the Java running system and the resource exhaustion. Applications should not throw this type of object (generally throwing by virtual machine). If this error occurs, in addition to trying to make the program security exit, it is impossible to force. So, when you print, you should pay more attention to the Exception system. 2.2.2 Exception System The except system includes the RuntimeException system and other non-RuntimeException systems 2.2.2.1 RuntimeExceptionRuntimeException system includes errors, type conversions, array crosstal access, and try to access empty pointers. The principle of processing RuntimeException is: If RuntimeException occurs, it must be a programmer's error. For example, you can avoid an array-proof accessibility to an array subscript and array boundaries. 2.2.2.2 Other (IOException, etc.) This exception is generally an external error, for example, attempts to read data from the end of the file, etc., this is not an error in the program itself, but an external error that appears in the application environment. 2.3 Different from C Abnormal Classification 2.3.1 In fact, RuntimeException in Java is not appropriate, because any exception occurs when running. (In compilation, it is not an abnormality, in other words, an exception is to resolve the error that occurs when the program is running). 2.3.2 C Logic_Error and RuntimeException in Java is equivalent, and Runtime_ERROR is equivalent to the exception of non-runtimeException type in Java.
3 Outlier Use 3.1 Declaration Methods Throw 3.1.1 Syntax: Throws () 3.1.2 Why do you declare that the method throws an exception? The method is as important as the type of abnormality is as important as the type of method returns. Suppose the method throws an exception. It does not declares that the method will throw an exception, then the customer programmer can call this method without writing the code for processing exceptions. So, once an exception occurs, this exception has no suitable abnormal controller to solve. 3.1.3 Why is the abnormality thrown? RuntimeException and Error can be generated in any code, which does not need to be thrown by the programmer, and once an error occurs, the corresponding exception will be thrown. It has been checked by the programmer, this is divided into two cases: the customer programmer call will throw an exception library function (the exception of the library function is thrown by the library "; the customer programmer should use the throw statement Exception. When you encounter Error, the programmer is generally impossible; if you encounter RuntimeException, it must be a logical error in the program. To modify the program (equivalent to a method of debugging); only the inspection exception is the programmer, The program should only throw or handle the exception. 3.1.4 Note: Covering the subclass of the parent class cannot throw more exceptions than the parent class method, so sometimes the method of designing the parent class will declare the abnormality, but the actual implementation method is also Do not throw an exception, the purpose of doing this is to throw an abnormality for the convenience of the child class. 3.2 How to throw an exception 3.2.1 Syntax: Throw () 3.2.2 What exception is thrown? For an abnormal object, an abnormal object type is really useful, and the abnormal object itself is meaningless. For example, an exception object is ClassCastException, then this type is the only useful information. So, when choosing something exception, the most critical is to select an exception class name to clearly illustrate the class of abnormal conditions. 3.2.3 Abnormal objects typically have two constructors: one is a parameter constructor; the other is a constructor with a string, which will be an additional description other than the type name other than the type name. 3.2.4 Creating its own exception: When the Java built-in exception cannot clearly explain the abnormal situation, you need to create your own exception. It should be noted that the only useful is the type name this information, so don't cost energy in the design of the anomaly. 3.3 Capture Exception If an exception is not processed, then for a non-graphical interface, the program will be aborted and output an exception information; for a graphical interface program, an exception is also output, but the program is not stopped Although returns to the user interface processing loop. 3.3.1 Syntax: TRY, CATCH and FINALLY controller modules must be followed by the TRY block. If a abnormality is thrown, the exception control mechanism will search for the first controller that the parameter and exception type can then go into that CATCH clause and considers exception to be controlled. Once the Catch clause ends the search for the controller, it will stop. 3.3.1.1 Capturing multiple exceptions (pay attention to grammar and capture order) (omitted) 3.3.1.2 Finally Usage and exception handling flow (omitted) 3.3.2 Abnormal processing? For Java, due to garbage collection, exception handling does not need to recycle memory. But there are still some resources that require programmers to collect, such as files, network connections, and pictures.