Types of exceptions in Java

Source: Internet
Author: User
Document directory
  • Java. lang. Throwable
  • Java. lang. Error
  • Java. lang. Exception
  • Java. lang. RuntimeException

Generally, exceptions in Java are divided into three types:

  1. Error: this exception is designed not to be captured because it is generated by the JVM itself.
  2. 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.
  3. 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 throwsClause
Any subclasses RuntimeExceptionThat might be thrown during the execution of the method but not caught. "the direct subclasses of java. lang. RuntimeException include:

  1. AnnotationTypeMismatchException
  2. ArithmeticException
  3. ArrayStoreException
  4. BufferOverflowException
  5. BufferUnderflowException
  6. CannotRedoException
  7. CannotUndoException
  8. ClassCastException
  9. Cmcmexception
  10. ConcurrentModificationException
  11. DataBindingException
  12. DOMException
  13. EmptyStackException
  14. EnumConstantNotPresentException
  15. EventException
  16. IllegalArgumentException
  17. IllegalMonitorStateException
  18. IllegalPathStateException
  19. IllegalStateException
  20. ImagingOpException
  21. IncompleteAnnotationException
  22. IndexOutOfBoundsException
  23. JMRuntimeException
  24. LSException
  25. MalformedParameterizedTypeException
  26. MirroredTypeException
  27. MirroredTypesException
  28. MissingResourceException
  29. NegativeArraySizeException
  30. NoSuchElementException
  31. Nosuchmachismexception
  32. Nullpointerexception
  33. Profiledataexception
  34. Providerexception
  35. Rasterformatexception
  36. Rejectedexecutionexception
  37. Securityexception
  38. Systemexception
  39. Typeconstraintexception
  40. Typenotpresentexception
  41. Undeclaredthrowableexception
  42. Unknownannotationvalueexception
  43. Unknownelementexception
  44. Unknowntypeexception
  45. Unmodifiablesetexception
  46. Unsupportedoperationexception
  47. 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:
  1. Error: errors related to JVM, such as resource deficiences, invariant failures, and other exceptions that make JVM abnormal.
  2. Runtime exception: used for programming errors. Many of the JDK's built-in functions are for programming errors.
  3. 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.