An error indicates that there is a problem with the program. An exception is a specific construct that interrupts the control flow of the program, and unwinds the stack, capturing information about the state of the stack so that it can be reported.
An exception can be used to indicate an error, but not always. For example:
void startOperation() {
try {
while (someComplexOperationIsOnGoing()) {
checkRestart();
}
}
catch (RestartException re) {
startOperation();
}
}
void checkRestart() {
if (shouldRestart()) {
throw new RestartException();
}
}
This incomplete code sample is meant to show a case where an exception is not an error. This is not always best practice; but it is used in some cases where the intent is to interrupt the control flow deep in the program (such as redirecting the page in a web framework, when responding to an HTTP request) and return control to a higher-up level of the stack. The term exception refers to the mechanism which interrupts the program.
In java, there is an Exception class which encapsulates this behavior. The Error class also interrupts the control flow in the same way as an Exception; but it is reserved only for serious, unrecoverable problems that happen at runtime. It is used, for example, when the JVM runs out of memory and can't create new objects.