In Java, all exceptions have a common ancestor Throwable (throwable). Throwable specifies the commonality of any problem in code that can be propagated through a Java application using an exception propagation mechanism.
Throwable: There are two important subclasses: Exception (Exception) and Error (Error), both of which are important subclasses of Java exception handling. Each contains a large number of subcategories. The difference between exceptions and errors is that exceptions can be handled by the program itself, while errors cannot be handled. (Recommended learning: java course)
Error (error): is an error that cannot be handled by the program, indicating a serious problem in running the application. Most errors have nothing to do with actions performed by the code writer and instead represent problems with the JVM (Java Virtual Machine) while the code is running.
For example, Java virtual machine running error (Virtual MachineError), when the JVM no longer has the memory resources required to continue executing the operation, an OutOfMemoryError will occur.
When these exceptions occur, the Java Virtual Machine (JVM) generally chooses to terminate the thread. These errors indicate that the fault occurs in the virtual machine itself or when the virtual machine attempts to execute an application, such as Java virtual machine running error (Virtual MachineError), class definition error (NoClassDefFoundError), etc.
These errors are uncheckable because they are outside the control and processing capabilities of the application program, and most of them are situations that are not allowed to occur when the program is running. For a well-designed application, even if an error does occur, there should be no attempt to handle the exception condition it caused. In Java, errors are described through subclasses of Error.
Exception: is an exception that the program itself can handle. The Exception class has an important subclass, RuntimeException. The RuntimeException class and its subclasses represent errors caused by "common JVM operations."
For example, if you try to use a null object reference, divide by zero, or the array is out of bounds, runtime exceptions (NullPointerException, ArithmeticException) and ArrayIndexOutOfBoundException will be thrown respectively.
Exception (exception) is divided into two categories: Run-time exception and non-run-time exception (compilation exception). The program should handle these exceptions as much as possible.
Runtime exceptions: are all exceptions of the RuntimeException class and its subclasses, such as NullPointerException (null pointer exception), IndexOutOfBoundsException (subscript out-of-bounds exception), etc. These exceptions are unchecked exceptions. You can choose to capture and process it in the program or not.
These exceptions are generally caused by program logic errors, and the program should try to avoid the occurrence of such exceptions from a logical perspective. The characteristic of runtime exceptions is that the Java compiler will not check it. That is to say, when this type of exception may occur in the program, even if it is not caught with a try-catch statement or declared to be thrown with a throws clause, it will Compiled and passed.
Non-runtime exception (compilation exception): is an exception other than RuntimeException, and all types belong to the 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 will not be compiled. Such as IOException, SQLException, etc. and user-defined Exceptions. Generally, no custom checked exceptions are required.
The above is the detailed content of Exceptions and errors in java. For more information, please follow other related articles on the PHP Chinese website!