posted 17 years ago
Hi chaitanya,
Let me tell you this way.
Checked or Compile-time Exceptions:
Checked Exceptions are kinda exceptions which the compiler is insisted to check during compilation as these exceptions are present in the throws clause of the method declaration syntax.
Say, for example you are doing any file related operations, definitely the operations are not always assured to be successful because either the file itself might not be present, or there might be any issues with the file attributes like permissions etc which may cause the disturbance to the operations on the file. Thats why the methods on File or related classes will have the corresponding exceptions appended with the "throws" clause as they are tentatively thrown. Some exceptions are like FileNotFoundException,EOFException etc. They are all the child class exceptions of IOException which are in java.lang package.
By any chance, the user/developer makes use of the API (methods) for dealing with file but without handling the exceptions, the compiler does not know what to do when such exception occurs. It can't inturn take the responsibility of handling it. Thereby it always expects and insists the user to be aware of these exceptions and ways to handle them. Probably he can abort the program, or retry with a custom message, call someother method during exception etc.
The user can handle the exceptions in two ways.
by surrounding the statements which contains the methods-may-throw-exceptions in a try-catch block by appending the probable exception classes in throws clause of the calling method
Thats why they are called *checked* or *compile-time* exceptions as the compiler is responsible for *checking* it during *compile-time*.
Unchecked or Runtime Exceptions:
You may ask, it more or less seems to be of runtime exceptions. But this has got nothing to do with the time when the exceptions are thrown. Because, certainly all exceptions are thrown at runtime only. As the term exception is defined, its an error preventing the running flow of an application. Right?
But the Runtime Exceptions are of different category, wherein the user is not insisted to be aware of the exceptions which may likely to be thrown at runtime thereby no need to handle them in the code.
Say for example, when you are dealing with an Array and the array is being accessed in a loop or with the value passed in command line argument for the index. In that case, the value might exceed by either case while accessing the array thereby an ArrayIndexOutOfBoundsException will be thrown. The user is not insisted to handle this and the compiler is also not asked to check for these kind of exceptions being handled in the code during compilation.
One other famous example of Runtime Exception is NullPointerException which is being thrown when there is an invocation on the null object reference.
Hope this helps to certain extent.
[ May 31, 2007: Message edited by: Raghavan Muthu ]