Check exceptions and non-check exceptions

Source: Internet
Author: User

Java exception detection and non-detection are distinct. The compiler verifies that an exception can be detected. For any method that declares that an exception is thrown, the compiler enforces the processing or declaration rules.

Non-detect exceptions do not follow the handling or declaration rules. When such exceptions are generated, the compiler does not check whether such exceptions have been resolved. There are two main classes that define non-detection exceptions: runtimeexception and error.

Why does the error subclass belong to a non-detection exception? This is because they cannot predict their production time. If the Java applicationProgramIf the memory is insufficient, an outofmemoryerror may occur at any time. The cause is generally not a special call in the application, but a JVM issue. In addition, the error class generally indicates that the application cannot solve a serious problem, so these classes are considered as non-detection exceptions.

The runtimeexception class is also a non-detect exception. One reason is that running exceptions caused by common JVM operations may occur at any time. Unlike error, such exceptions are generally caused by specific operations. However, these operations frequently occur in Java applications. For example, if you use an object every time, you must write Exception HandlingCodeTo check for null references, the entire application will soon become a large try-Catch Block. Therefore, runtime exceptions are not restricted by compiler checks and processing or declaration rules.

There is another reason for using the runtimeexception class as an undetected exception: the issues they indicate are not necessarily handled as exceptions. Nullpointerexception can be processed in the try-catch structure. However, it is simpler and more economical to test null values before reference is used. Similarly, you can check the value 0 during Division without using arithmeticexception.

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.