What kind of Java exceptions inherit, and the difference between runtime exceptions and general exceptions (detailed explanation)

1、 Basic concepts

Throwable is the root of all exceptions, Java lang.Throwable

Error is an error, Java lang.Error

Exception is an exception, Java lang.Exception

Throwable: there are two important subclasses: exception and error. Both are important subclasses of Java exception handling, and each contains a large number of subclasses.

Error: it is an error that cannot be handled by the program, indicating a more serious problem in running the application. Most errors have nothing to do with the operation performed by the coder, but indicate a problem with the JVM (Java virtual machine) when the code is running. For example, Java virtual machine running error (virtual machineerror). Outofmemoryerror will appear when the JVM no longer has the memory resources required to continue the operation. When these exceptions occur, the Java virtual machine (JVM) will generally choose thread termination.

These errors indicate that the failure occurred in the virtual machine itself or when the virtual machine tried to execute the application, Such as Java virtual machine running error and class definition error (NoClassDefFoundError), etc. these errors are not traceable because they are outside the control and processing capacity of the application, and most of them are conditions that are not allowed to occur when the program is running. For a reasonably designed application, even if an error does occur, it should not try to deal with the abnormal conditions caused by it. In Java, errors are common Subclass description of error.

Exception: an exception that can be handled by the program itself.

The exception class has an important subclass runtimeException. The runtimeException class and its subclasses represent errors caused by "JVM common operations". For example, if you try to use a null object reference, divide by zero, or array out of bounds, run-time exceptions (NullPointerException, arithmeticexception) and arrayindexoutofboundexception are thrown, respectively.

Note: the difference between exceptions and errors: exceptions can be handled by the program itself, while errors cannot be handled.

2、 Exception

It is generally divided into checked exceptions and runtime exceptions. All instances of runtimeException class and its subclasses are called runtime exceptions, and exceptions that do not belong to this category are called checkedexceptions.

① Checked exception

Only the Java language provides checked exceptions. Java believes that checked exceptions can be handled, so Java programs must display and handle checked exceptions. If the program does not handle the checked exception, an error will occur when the program is compiled and cannot be compiled. This reflects Java's design philosophy: code without perfect error handling has no chance to be executed at all. There are two ways to handle checked exceptions

1. If the current method knows how to handle the exception, try Catch block to handle the exception. 2 if the current method does not know how to handle it, the exception is thrown when the method is defined.

We are familiar with checked exceptions

Java. lang.ClassNotFoundException Java. lang.NoSuchMetodException java. io. IOException

②RuntimeException

At runtime, if the divisor is 0 and the array subscript is out of bounds, it is frequent and troublesome. If the declaration or capture is displayed, it will have a great impact on the readability and running efficiency of the program. Therefore, the system automatically detects them and gives them to the default exception handler. Of course, if you have processing requirements, you can also display and capture them.

The familiar subclasses of rumtimeexception are

Java. lang.ArithmeticException Java. lang.ArrayStoreExcetpion Java. lang.ClassCastException Java. lang.indexoutofboundsexception Java. lang.NullPointerException

3、 Error

When an uncontrollable error occurs in the program, the common practice is to notify the user and abort the execution of the program. Unlike exceptions, objects of error and its subclasses should not be thrown.

Error is a subclass of throwable, which represents compilation time and system errors. It is used to indicate serious problems that reasonable applications should not try to catch.

Error is generated and thrown by Java virtual machine, including dynamic link failure, virtual machine error, etc. The program does not process it.

What kind of Java exceptions inherit and the difference between runtime exceptions and general exceptions (detailed explanation) are all the contents shared by Xiaobian. I hope to give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>