Mistake indicates a problem with the program. an exception is a particular construct that interrupts the program control flow 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 sample code is intended to show the case where the exception is not an error. This is not always best practice; but it is used in some cases where the goal is to interrupt the control flow deep into the program (for example, redirecting a page in the web infrastructure when responding to an HTTP request) and controlling the return to a higher level of the stack. The term exception refers to the mechanism that interrupts the program .
In java, there is an Exception class that encapsulates this behavior. The Error class also interrupts the control flow in the same way as the Exception; but it is reserved only for serious, fatal problems that occur at runtime. It is used, for example, when the JVM runs out of memory and cannot create new objects.
RMorrisey
source share