Java basics-exceptions, Java basics --
People who often write programs must be familiar with try... catch... finally statements. However, many people are not very clear about the exception. They do not know what it means or why it is used for exception handling. This blog analyzes the working principles of exception handling in java in essence.
I. Definition
In java programming ideology, an exception is defined as an exception that prevents the execution of the current method or scope from continuing.
This is a written language. Translation into our own language is actually a problem with the program running. Specifically, a program running problem may cause the program running mechanism. Therefore, an exception is thrown to tell developers where the problem has occurred.
Ii. Exception System
1. Since the problem arises, it is necessary to solve the problem, just like a person is ill and the right remedy. java also gives us a series of exception handling classes. How should these classes be differentiated?
In general, there are two types of exceptions in java: one is Error and the other is Exception.
Generally, error cannot be handled, which is also called an error. Exception can be handled.
2. Exception Classification
Exceptions can be classified into two types: runtime exceptions and compilation exceptions.
A. runtime exception:
All are RuntimeException classes and their subclass exceptions, such as NullPointerException (NULL pointer exception) and IndexOutOfBoundsException (subscript out-of-bounds exception). These exceptions are non-check exceptions and can be captured and processed in programs, or not. These exceptions are generally caused by program logic errors. The program should avoid such exceptions logically.
The runtime exception is characteristic that the Java compiler does not check it. That is to say, when such exceptions may occur in the program, even if the try-catch statement is not used to catch it, it is not thrown by the throws clause declaration, it will also be compiled.
B. Non-runtime exception (compilation exception ):
Is an Exception other than RuntimeException, and belongs to the Exception class and its subclass. From the perspective of program syntax, it is an exception that must be processed. If it is not processed, the program cannot be compiled. Such as IOException, SQLException, and user-defined Exception. Generally, no custom check Exception occurs.
Note: in java, both the Exception and Error sub-classes use the parent class name as the suffix.
Iii. custom exception classes
Since the exception handling in java is a pre-built class called, can we customize the exception class? The answer is yes.
1 class CeshiException extends Exception 2 { 3 CeshiException() 4 {} 5 6 7 CeshiException(String msg) 8 { 9 super(msg);10 }11 }Iv. Exception Handling principles
1. If the function content throws an exception to be detected, it must be declared on the function; otherwise, it must be captured using try... catch in the function; otherwise, the compilation fails.
2. If you call a function that declares an exception, either try... catch or throws. Otherwise, the compilation fails.
3. If a function throws multiple exceptions, multiple catch statements must be called for targeted processing. If there are several internal exceptions that need to be detected, you can throw a few exceptions and throw a few to catch several exceptions.
V. Exception Handling Mechanism
In Java applications, the exception handling mechanism is: throw an exception and catch an exception.
1. Throw an exception: When a method error causes an exception, the method creates an exception object and delivers it to the runtime system, the exception object contains exception information such as the exception type and program status when the exception occurs. The runtime system is responsible for finding and executing the code to handle exceptions.
2. Capture exceptions: After a method throws an exception, the system will convert it into an exception handler ). A potential exception processor is a set of methods that are stored in the call stack in sequence when an exception occurs. When the exception type that the exception processor can handle matches the exception type thrown by the method, it is a suitable exception processor. During the runtime, the system starts from the abnormal method and re-queries the method in the stack until it finds and executes the method containing the appropriate exception processor. When the runtime system traverses the call stack and does not find a suitable exception processor, the runtime system terminates. At the same time, it means the termination of the Java program.
6. Conclusion
The exception handling framework of a program directly affects the code quality of the entire project, as well as the maintenance cost and difficulty. Using exception handling can make our program robust.