why exception in java has been classified into checked and unchecked
exception rather than keeping it under simply exception?
There are actually 3 types. Checked Exceptions, Runtime Exceptions and Errors. Errors are similar to Exceptions but they are reserved for VM errors like out of memory conditions.
When you have a method, it's name usually indicates what it does, one of the reason for having checked exceptions is to indicate what the method
can't do.
Example:
So for someone using this method what is it saying?
readFile: Hi, I'm readFile. If you pass a file name
string to me I can read it and return the contents as a string. But if the file's not there, I'm sorry but I can't handle that. I'm just giving up and throwing it back at you. Really sorry, but I just can't deal with this situation.
The reason is that this method might be common and could be used with a web application, web service, desktop app, cell phone, etc. Each might would want to hand this situation differently.
This is a simple example. There are many cases where you might not want to throw an exception. This is one of the great debates. If you google you'll find many articles and threads on this topic.