Java exception 01 (Error and Exception)

What is abnormal

In actual work, you will always encounter some special situations, such as: a certain module you write, user input may not meet your requirements, your program needs to open a certain file, this file may not exist or the file format is wrong, you To read the data of the database, the data may be empty, the program is running, the memory or hard disk is full, etc.

When the software program is running again, it is very likely to encounter these abnormal conditions just now. We call it an exception. The English is: Exception, which means that for these exceptions, we must deal with the program reasonably so that the program does not crash.

Simple classification
1. Checked exceptions: The most representative checked exceptions are exceptions caused by user errors or problems, which cannot be foreseen by programmers. For example, to open a file that does not exist, an exception occurs, and these exceptions cannot be simply ignored during compilation.
2. Runtime exceptions: Runtime exceptions are exceptions that may be avoided by programmers. In contrast to checked exceptions, runtime exceptions can be ignored at compile time.
3. ERROR: The error is not an exception, but a problem out of the control of the programmer. Errors are usually ignored in the code. For example, when the stack overflows, an error occurs, and they cannot be checked at compile time.

Exception architecture
1. Java treats exceptions as objects, and defines a base class java.lang.Throwable as the superclass of all exceptions
2. Many exception classes have been defined in the Java API, and these exception classes are often divided into two Major categories, Error and Exception

Error
1. The Error class object is generated and thrown by the Java virtual machine. Most errors have nothing to do with the operation performed by the code writer. 2. The Java virtual machine
is running error (Virtual MachineError), when the JVM no longer needs to continue to suffocate. The memory resource is, OutOfMemoryError will occur. When these exceptions occur, the Java Virtual Machine (JVM) generally chooses the thread to terminate;
3. There are also occurrences when the virtual machine tries to execute the application, such as class definition error (NoClassDefFoundError), link error (LinkageError). These errors are sometimes undetectable because they are outside the control and processing capabilities of the application program, and most of the time the program is not allowed to appear when the program is running.

Exception

There is an important subclass RuntimeException in the Exception branch.
1.ArrayIndexOutOfBoundsException (array index out of bounds)
2.NullPointerException (null pointer exception)
3.ArithmeticException (arithmetic exception)
4.MissingResourceException (lost resource)
5.ClassNotFoundException (Cannot find the class) and other exceptions, these exceptions are not checked exceptions, the program can choose to capture processing, or not to handle

These exceptions are generally caused by program editing errors, and the program should avoid the occurrence of such exceptions as much as possible from a logical point of view;

The difference between Error and Exception: Error is usually a catastrophic fatal error that cannot be controlled and handled by the program. When these exceptions occur, the Java Virtual Machine (JVM) generally chooses to suspend the thread; Exception can usually be handled by the program , And should deal with these exceptions as much as possible in the program.

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/114100133