I don't understand the difference between error and exception. Somebody says the error is thrown by JVM and the exception is thrown by programmers but what about the RuntimeException (subclasses thrown by JVM) and AssertionError (thrown by programmers for debugging purpose)???
Exceptions: Exceptions are any error or any invalid conditions that occur during the execution of program whenever any exception occure an exception object is created and thrown that contains the informatin about the information like the name of that exception or the complete stack trace which provides the information about all the methods that have been called till the time exception occur.
Exceptions can be of two type
Unchecked exceptions : All the exceptions that come under RuntimeExceptions are unchecked exceptions. These exceptions are used to denote any error that exist in the program logic. Any method that can throw the unchecked exceptions does not need to provide a explicit exception handling mechanism for those exceptions. .(do not need to be handled explicitly)
Checked Exceptions: All the exceptions that are subclasses of Exception except Runtime exception are checked exceptions. These exceptions are generally used to represent any invalid condition that occur during the execution of program. Any method that may throw the checked exception should provide a explicit exception handling mechanism to deal with those exceptions otherwise there will be a compile time error.(need to be handled explicitly)
Error: Error defines the exceptions that are not expected to be caught by the program. Like stack Overflow. Recovery from them is difficult or impossible.
Sudhakar Sharma wrote:. . . Somebody says the error is thrown by JVM and the exception is thrown by programmers . . .
They are mistaken. But it is possible intentionally to throw an Exception or Error or Throwable with the throw keyword.
posted 8 years ago
Sudhakar Sharma wrote: . . . AssertionError (thrown by programmers for debugging purpose) . . .
It is not normal to throw an AssertionError, but to use the keyword assert, which you correctly say is used for debugging, or more precisely for checking correctness of the program. I think the AssertionError is a bit of a "cheat" because catch (Exception e) . . . is a common programming construct and the Error passes that, a bit like an unchecked Exception.