Possible Duplicate:
Differences betweeen Exception and Error
How can I differentiate between Errors and Exceptions in Java?
Possible Duplicate:
Differences betweeen Exception and Error
How can I differentiate between Errors and Exceptions in Java?
while
Error along with
RuntimeException
& their subclasses areunchecked
exceptions. All other Exception classes arechecked
exceptions.Checked exceptions are generally those from which a program can recover & it might be a good idea to recover from such exceptions programmatically. Examples include
FileNotFoundException
,ParseException
, etc. A programmer is expected to check for these exceptions by using the try-catch block or throw it back to the callerOn the other hand we have unchecked exceptions. These are those exceptions that might not happen if everything is in order, but they do occur. Examples include
ArrayIndexOutOfBoundException
,ClassCastException
, etc. Many applications will usetry-catch
orthrows
clause forRuntimeExceptions
& their subclasses but from the language perspective it is not required to do so. Do note that recovery from aRuntimeException
is generally possible but the guys who designed the class/exception deemed it unnecessary for the end programmer to check for such exceptions.Errors are also unchecked exception & the programmer is not required to do anything with these. In fact it is a bad idea to use a
try-catch
clause for Errors. Most often, recovery from an Error is not possible & the program should be allowed to terminate. Examples includeOutOfMemoryError
,StackOverflowError
, etc.Do note that although Errors are unchecked exceptions, we shouldn't try to deal with them, but it is ok to deal with
RuntimeExceptions
(also unchecked exceptions) in code. Checked exceptions should be handled by the code.Error
andException
both extendThrowable
, but mostlyError
is thrown by JVM in a scenario which is fatal and there is no way for the application program to recover from that error. For instanceOutOfMemoryError
.Though even application can raise an
Error
but its just not a good a practice, instead applications should use checked exceptions for recoverable conditions and runtime exceptions for programming errors.In general error is which nobody can control or guess when it occurs.Exception can be guessed and can be handled. In Java Exception and Error are sub class of Throwable.It is differentiated based on the program control.Error such as OutOfMemory Error which no programmer can guess and can handle it.It depends on dynamically based on architectire,OS and server configuration.Where as Exception programmer can handle it and can avoid application's misbehavior.For example if your code is looking for a file which is not available then IOException is thrown.Such instances programmer can guess and can handle it.
Error is something that most of the time you cannot handle it.
Exception was meant to give you an opportunity to do something with it. like try something else or write to the log.