2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Checked vs Unchecked Exceptions in Java

I am newly started learning Exception handling in java and got some doubts something related to Checked and Unchecked exception. like,

1. Both are useful at run time then why we call Checked exception checked at compile time.

2. If we can use both at run time then what is the necessity of two why not one.

3. Java provide a lot of Exception classes, then how we can Identify which is checked and unchecked.

Your valuable time to clear my doubts is highly appreciated.


Yes, you are right both are helpful at run time. But based on the scenario Java guys divided the exceptions into two phases i.e Checked and Unchecked.

As we know prevention is better than cure, Checked exception is something like that. It will checked by at compile time itself, but it doesn't mean that it handle the exception at compile time. It will just bypass by saying that yes in code execution there is chance of getting that exception and the developer handle it properly.

Some exception which is compulsory need to handle and some of them optional. Because of this reason we have two kinds of exceptions. For more you can refer to http://www.geeksforgeeks.org/checked-vs-unchecked-exceptions-in-java/ also.

how we can Identify which is checked and unchecked.

The class RuntimeException and its subclasses, and the class Error and its subclasses are unchecked exceptions classes. Because the compiler doesnÂ’t forces them to be declared in the throws clause. All the other exception classes that are part of Throwable hierarchy are checked exceptions.

Answer is