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