Checked exception and unchecked exception in Java

Source: Internet
Author: User

checked exception and unchecked exception in Java

There are two kinds of exceptions in Java: Checked exception and unchecked exception.

Checked Exception

Checked exception is this definition:

A checked exception is an exception that must being either caught or declared in a method where it can be thrown.

That is, we have to write the code must be caught or thrown exception, it will be found during the compilation period, forcing the corresponding processing, Checked exceptions subclass is inherited Java.lang.Exception, such as filenotfoundexception,parseexception, are checked Exception, and you must use Try...catch (or throws) to catch an exception.

Unchecked Exception

Unchecked exception is defined in this way;

Unchecked, uncaught or runtime Exceptions was exceptions that was not required to be caught or declared, even if it was all owed to doing so.

Unchecked exception inherits from RuntimeException or error, unchecked exception refers to the exception that should have happened in all normal programs, such as Arrayindexoutofboundexception, Classcastexception,nullpointerexception, the program should not go catch such exceptions, such exceptions are generally code problems, need to repair.

Then when do we choose checked exception, when to choose unchecked exception? The official documentation gives the following rules:

Here's the bottom line guideline:if a client can reasonably is expected to recover from an exception and make it a checked E Xception. If A client cannot does anything to recover from the exception, make it an unchecked exception.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Checked exception and unchecked exception in Java

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.