Transferred from: https://www.jianshu.com/p/e8bbee3c1c4a
The parent classes of the error class and the exception class are throwable classes, the difference being:
The error class generally refers to problems related to virtual machines, such as system crashes, virtual machine errors, insufficient memory space, method call stack overflow, and so on. For this kind of error caused by the application interruption, only by the program itself cannot be recovered and prevented, encountered such errors, it is recommended to let the program terminate.
The exception class represents exceptions that the program can handle and can be captured and possibly recovered. If you encounter such an exception, you should handle the exception as much as possible so that the program resumes running, rather than terminating the exception arbitrarily.
The Exception class is also divided into runtime exceptions (runtime Exception) and checked exceptions (Checked Exception), run-time exceptions; Arithmaticexception,illegalargumentexception, the compilation can pass, but a run is terminated, the program will not handle runtime exceptions, such exceptions, the program will terminate. The exception to be checked is either a try ... Catch capture, either thrown with the throws clause declaration, to its parent class, or the compilation does not pass.
①.exception (Exception) is a possible predictable, recoverable issue in your application. Most exceptions generally represent moderate to mild problems. Exceptions are typically generated in a specific environment, and often appear in specific methods and operations of the code. In the Echoinput class, a IOException exception may occur when attempting to invoke the ReadLine method.
The Exception class has an important subclass runtimeexception. The RuntimeException class and its subclasses represent errors thrown by the JVM common operation. For example, if you attempt to use null object references, divide by zero, or array out of bounds, the run-time exceptions (NullPointerException, ArithmeticException), and arrayindexoutofboundexception are raised respectively.
②.error (Error) indicates a more serious problem in running the application. Most errors are unrelated to what the code writer does, and represent a problem with the JVM (the Java virtual machine) that is running the code. For example, outofmemoryerror occurs when the JVM no longer has the memory resources required to continue the operation.
Checking for the division of exceptions and unchecked exceptions
Exceptions in Java fall into two main categories:
1.Checked Exception (non-runtime Exception)
2.Unchecked Exception (Runtime Exception)
run-time exception
The RuntimeException class is a subclass of the exception class, which is called a run-time exception, and all runtime exceptions in Java are inherited directly or indirectly from the RuntimeException class.
Exceptions in Java that inherit from exception and do not inherit from the RuntimeException class are non-runtime exceptions.
A try can be followed by multiple catch, but no matter how many, only one catch block will be executed at most.
For non-runtime exceptions (checked exception), it must be processed, otherwise it cannot be compiled.
There are two ways of handling this:
1. Use try: Catch.. finally to capture;
2. After the method declaration that generates the exception, write the throws a Exception type, such as throws Exception, and throw the exception to the outside layer.
For runtime exceptions (runtime exception), they can be processed or not handled. It is not recommended to handle run-time exceptions.
Extensions: Differences between errors and exceptions (Error vs Exception)
1). Java.lang.Error:A subclass of Throwable that is used to mark critical errors. A reasonable application should not go try/catch this error. The vast majority of errors are abnormal and should not be seen at all.
A subclass of java.lang.Exception:Throwable that is used to indicate a reasonable program that wants to catch a condition. That is, it is only a program run condition, not a critical error, and encourages the user program to catch it.
**2). Error and RuntimeException * * and its subclasses are unchecked exceptions (unchecked exceptions), and all other exception classes are checked exceptions (checked exceptions).
Checked exceptions: Usually thrown out of a program that can be recovered, and it is best to use program recovery from this exception. such as FileNotFoundException, ParseException and so on. The checked exception occurs during the compile phase and must be used Try...catch (or throws) or the compilation will not pass.
**unchecked Exceptions: * * It's usually the exception if all the normal scripts don't happen, but it does happen. Occurs in the running period, with uncertainty, mainly due to the logic problems of the program. such as Arrayindexoutofboundexception, classcastexception and so on. From the point of view of the language itself, the program should not catch such exceptions, although it can catch and recover from exceptions such as runtimeexception, but does not encourage the terminal programmer to do so, because it is not necessary at all. Because such errors are bugs in themselves, they should be fixed and the program should stop executing immediately when such errors occur. Therefore, in the face of errors and unchecked exceptions should let the program automatically terminate execution, programmers should not do things such as try/catch, but should find out why, modify the code logic.
runtimeexception:The runtimeexception system includes wrong type conversions, array cross-border access, and attempts to access null pointers and so on.
The principle of dealing with runtimeexception is that if a runtimeexception occurs, it must be a programmer's fault. For example, you can avoid array out-of-bounds access exceptions by examining array subscripts and arrays boundaries. Other (IOException, etc.) checked exceptions are generally external errors, such as trying to read data from the end of a file, which is not an error in the program itself, but an external error that occurs in the application environment.
Above this article Java_ exception class (Error and exception, the difference between the two introduced) is a small part of the whole content to share to everyone, I hope to give you a reference, but also hope that we support the script home.
A common anomaly;
ArrayIndexOutOfBoundsException array Subscript out-of-bounds exception,
Arithmaticexception arithmetic exceptions such as dividing by zero
NullPointerException NULL pointer exception
IllegalArgumentException Illegal parameter exception
Ai Xiaotian Son
Links: https://www.jianshu.com/p/e8bbee3c1c4a
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.
The difference between error and exception in Java