What is the difference between Exception and Error?

Most of the people think that Exception causes at run-time and Error causes at compile time. But here are we are going to discuss about run-time exceptions and errors.
Throwable is the root class for exception hierarcy and Exception and Error are the subclasses of throwable class.

Exception:

Exception is the subclass of throwable class. Exceptions are caused by the program itself . We can recover a exception by handling the exception using try-catch block.
Example: 
If we are trying to read a file from a particular location and if the file is not available in that location at run-time, then the program will throw an exception. We can handling the exception and can provide a local file to continue the program.

Error:

Error is the subclass of throwable class. Errors are not caused by the program but due to lack of system resources and these are non-recoverable.
Example:
out of memory error due to lack of heap size.

No comments:

Post a Comment