In Java, all exceptions have a common ancestor, Throwable (throwable). Throwable specifies the commonality of any problems that can be transmitted through Java applications in the code by exception propagation mechanisms.
Throwable has two important subclasses: Exception and Error. Both are important subclasses for Java exception handling, and each contains a large number of subclasses.
①.Exception is a possible predictable and recoverable problem in the application. Generally, most abnormalities indicate moderate to mild problems. Exceptions are generally generated in specific environments and usually occur in specific methods and operations of the code. In the EchoInput class, an IOException may occur when trying to call the readLine method.
The Exception class has an important subclass, RuntimeException. The RuntimeException class and its subclasses represent an error raised by "Common JVM Operations". For example, if you try to use a null object reference, zero divisor or array out of bounds, a runtime exception (NullPointerException, ArithmeticException) and ArrayIndexOutOfBoundException are respectively raised.
②.Error indicates a serious problem in running the application. Most errors are not related to the actions performed by the code writer, but represent problems with the JVM (Java virtual machine) when the code is run. For example, when the JVM no longer has the memory resources needed to continue the operation, an OutOfMemoryError will appear.
The division between checking exceptions and not checking exceptions
Exceptions in Java are divided into two categories:
1.Checked Exception (non-Runtime Exception)
2.Unchecked Exception (Runtime Exception)
Runtime exception
The RuntimeException class is a subclass of the Exception class. It is called a runtime exception. All runtime exceptions in Java will be inherited directly or indirectly from the RuntimeException class.
Any exceptions in Java that inherit from Exception but not from RuntimeException are non-runtime exceptions.
A try can be followed by multiple catches, but no matter how many, at most one catch block will be executed.
For non-runtime exceptions, they must be processed, otherwise they cannot be compiled.
There are two ways to deal with it:
1. Use try..catch..finally to capture;
2. Write a throws Exception type after the method declaration that generates the exception, such as throws Exception, throw the exception to the outside layer.
For runtime exceptions, they can be processed or not. It is recommended not to handle runtime exceptions.
Extension: The difference between error and exception (Error vs Exception)
1).java.lang.Error: A subclass of Throwable, used to mark critical errors. A reasonable application should not go to try/catch errors. Most of the errors are abnormal and should not happen at all.
java.lang.Exception: A subclass of Throwable, used to indicate a reasonable program to catch. That is, it is just a program running condition, not a serious error, and user programs are encouraged to catch it.
2). Error and RuntimeException and their subclasses are unchecked exceptions, while all other Exception classes are checked exceptions.
checked exceptions: is usually thrown from a recoverable program, and is best able to recover from such exceptions using the program. For example, FileNotFoundException, ParseException, etc. The checked exception occurs in the compilation stage, and you must use try...catch (or throws) or the compilation will not be passed.
unchecked exceptions: Usually an exception that shouldn't have happened if everything is normal, but it does happen. It occurs during the runtime and has uncertainty, mainly caused by logical problems in the program. For example, ArrayIndexOutOfBoundException, ClassCastException, etc. From the perspective of the language itself, programs should not use exceptions like catch. Although they can catch and recover from exceptions such as RuntimeException, terminal programmers are not encouraged to do this because there is no need to do so. Because this type of error is a bug itself and should be fixed, the program should be stopped immediately when such errors occur. Therefore, in the face of Errors and unchecked exceptions, the program should be automatically terminated. Programmers should not do things such as try/catch, but should find out the reason and modify the code logic.
RuntimeException: The RuntimeException system includes wrong type conversion, array out-of-bounds access, attempts to access null pointers, etc.
The principle of handling RuntimeException is: If a RuntimeException occurs, it must be a programmer's error. For example, array out-of-bounds access exceptions can be avoided by checking array subscripts and array boundaries. Other (IOException, etc.) checked exceptions are generally external errors, such as trying to read data from the end of the file, etc. This is not an error of the program itself, but an external error that occurs in the application environment.
The above article Java_Exception class (introduction to the difference between errors and exceptions) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.