Detailed explanation of Java exception architecture
1、 What is an exception
Exception: a program exception event caused by hardware equipment problems, software design errors, etc. during the operation of the program. (in object-oriented programming languages such as Java) the exception itself is an object, and generating an exception is generating an exception object
2、 Abnormal system
Java treats exceptions as objects and defines a base class java Lang. throwable is the superclass of all exceptions. Many exception classes have been defined in the Java API. These exception classes are divided into two categories: error and exception. The exception class exception is divided into runtime exception and non runtime exception, which are very different, also known as unchecked exception
And checked exception.
The Java exception architecture is shown in the figure below:
1. Error and exception
Errorrors are often very serious errors and exceptions that cannot be handled by the program. They can be caught, but it is best not to catch them, because they cannot be solved. This is not produced by the program. If there are problems at the bottom, let them hang up, such as memory overflow (outofmemoryerror), thread death, etc. When these exceptions occur, the Java virtual machine (JVM) generally selects thread termination.
The following figure shows the memory overflow scenario simulated by the program:
Exceptions are exceptions that can be handled by the program itself. These exceptions are divided into two categories: runtime exceptions and non runtime exceptions. The program should handle these exceptions as much as possible.
2. Runtime and non runtime exceptions
Runtime exceptions are runtimeException class and its subclasses. Common exceptions include null pointer exception, index out of bounds exception, arithmetic exception, ClassCastException, etc. these exceptions are generally caused by program logic errors, The program should avoid such exceptions as much as possible from a logical point of view.
The following will simulate the scenarios in which these runtime exceptions occur:
1) Array subscript out of bounds exception
Take the array subscript out of bounds exception as an example to analyze the phenomenon. When the program is running, the JVM can detect that there is no a [3] index. When the array subscript out of bounds exception is detected, the JVM does two things:
1. Create the exception object new ArrayIndexOutOfBoundsException (3);
2 throw the exception object to its caller. Note: once an exception is thrown, the following program will not be executed.
When its caller does not handle the exception, its caller continues to throw the exception up. By analogy, when main receives this exception and does not handle the array subscript out of bounds exception, main throws the exception to the JVM. After receiving the exception information, the JVM does two more things:
1. Output the exception information to the console in red font;
2 stop the program.
2) Null pointer exception
3) Arithmetic exception divisor is 0
4) Type conversion exception
Non runtime exceptions are exceptions other than runtimeException, which belong to exception class and its subclasses. From the perspective of program syntax, it is an exception that must be handled. If it is not handled, the program cannot be compiled, such as IO exception, SQL (sqlexception) and user-defined exception.
The following figure shows FileNotFoundException in IOException:
summary
The above is a detailed explanation of the Java exception architecture introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!