Document directory
- Java. lang. Throwable
- Java. lang. Error
- Java. lang. Exception
- Java. lang. RuntimeException
Generally, exceptions in Java are divided into three types:
- Error: this exception is designed not to be captured because it is generated by the JVM itself.
- Runtime Exception: Runtime exceptions are often related to the environment, which cannot be checked during compilation and may occur in a wide range of situations. Therefore, the system will process the exceptions and the program does not need to be captured.
- Common exceptions: Most Common exceptions belong to this type.
The Java exception here refers to the direct inheritanceJava. lang. ThrowableTheir structure is as follows:
- Java. lang. Throwable
- Java. Lang. Error
- Java. Lang. Exception
- Java. Lang. runtimeexception
Java. lang. Throwablejava. lang. Throwable is the parent class of all errors and exceptions in Java. It is designed as a parent class rather than an interface here. I think part of the reason may be that, in the early days of the birth of Java, the use of class inheritance structures is more popular. But the more important reason is that Exception is not suitable for the interface design. The interface focuses on the description of implementation methods and rules, while Exception focuses on the information contained in it and the class name. The subclass of Throwable generally contains two constructors: the constructor with null parameters and the constructor with String parameters with exception information. If this class is inherited from other Exception classes, there will be two more constructor: constructor with the Throwable parameter and description String parameter. Java. lang. Errorjava. lang. Error occurs when the application should not try to capture it. Java programs do not need to throw or catch such classes and their subclasses, because such exceptions should not be handled by the application and are generally in the case of abnormal. Java. lang. Exceptionjava. lang. Exception refers to the exceptions that Java programs should catch. Java. lang. RuntimeException is a special subclass. Java. lang. RuntimeExceptionJava program should capture, but can not catch an exception. In most cases, it will not be captured. An important reason is that this exception may happen too often, and almost every line of code will have the risk of RuntimeException, so it does not need to be captured. The original statement in the JDK document is: "A method is not required to declare in its
throws
Clause
Any subclasses
RuntimeException
That might be thrown during the execution of the method but not caught. "the direct subclasses of java. lang. RuntimeException include:
- AnnotationTypeMismatchException
- ArithmeticException
- ArrayStoreException
- BufferOverflowException
- BufferUnderflowException
- CannotRedoException
- CannotUndoException
- ClassCastException
- Cmcmexception
- ConcurrentModificationException
- DataBindingException
- DOMException
- EmptyStackException
- EnumConstantNotPresentException
- EventException
- IllegalArgumentException
- IllegalMonitorStateException
- IllegalPathStateException
- IllegalStateException
- ImagingOpException
- IncompleteAnnotationException
- IndexOutOfBoundsException
- JMRuntimeException
- LSException
- MalformedParameterizedTypeException
- MirroredTypeException
- MirroredTypesException
- MissingResourceException
- NegativeArraySizeException
- NoSuchElementException
- Nosuchmachismexception
- Nullpointerexception
- Profiledataexception
- Providerexception
- Rasterformatexception
- Rejectedexecutionexception
- Securityexception
- Systemexception
- Typeconstraintexception
- Typenotpresentexception
- Undeclaredthrowableexception
- Unknownannotationvalueexception
- Unknownelementexception
- Unknowntypeexception
- Unmodifiablesetexception
- Unsupportedoperationexception
- Webserviceexception
Java Exception is divided into three types in Item 58 of objective Java: Error, checked Exception, and runtime exception. When designing a class, it is better to use these three exceptions as follows:
- Error: errors related to JVM, such as resource deficiences, invariant failures, and other exceptions that make JVM abnormal.
- Runtime exception: used for programming errors. Many of the JDK's built-in functions are for programming errors.
- Checked exception: an exception that can be recovered.
Because errors are usually used for JVM-related errors, other unchecked exceptions should be inherited from Runtime exceptions.