The difference between checkexception and uncheckexception in Java

Source: Internet
Author: User
In Java, the exception is divided into two, run-time exceptions (that is, uncheckexception) and checked exception checkexception, run-time exceptions, including the usual encountered various exceptions, such as null pointer exceptions, data format anomalies, such as a series of exceptions, this exception can not be captured, You can throw an exception via throws, and leave it to another code or Java Virtual machine to do it. However, checkexception must be handled, that is, cannot be thrown, must be done through the try-catch, do not deal with the words can not be compiled. But Checkexception is not a specific exception type, it's just a concept.     So you decide whether an exception has been checked for an exception or unchecked, just by throwing the exception. When the program is abnormal, virtual opportunity to help produce an exception object ... The following classes are provided by the JDK ...

where RuntimeException +error and its subclasses belong to the uncheckexception exception classes except runtimeexception are all belong to Checkexception

As you can see, the Setage method of the user class throws a Run-time runtimeexception that does not require an explicit throws after the method, and does not require a try/catch capture process when the client class is invoked. The Setage method for the checkuser class is to throw a exception (belonging to Checkexception), which requires an explicit throw exception after the Setage method, or a exception that appears after the try/definition Catch to capture and process, when the customer class calls, you also need to explicitly use try/catch capture processing, if not processed, you need to display the client class call its checkuser object after the method explicitly throw the exception, to the superior customer class capture or continue to throw up.

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.