Difference Between Exception and Error

Unexpected behavior is bound to occur when a program is running. This could be due to exceptions or errors. Exceptions are events, which can disturb the normal program flow. Errors are conditions that can be considered irrecoverable. Exceptions are mostly related to the application itself, while errors are related to the system on which the program is running.

What is an Exception?

Exception is an event, which can disturb the normal program flow. The name exception comes from “exceptional event”. Throwing an exception is the process of creating an exception object and handing it off to the runtime system. Exception object is created by the method in which the exception occurred. Exception object contains useful information such as the type and the description of the exception. When the runtime system receives the exception object, it will try to find somebody to handle it within the call stack by traversing it in the reverse order (in which the methods were called). Call stack is the ordered list of methods, which were called prior to the method in which the exception occurred. The runtime system is successful if it finds a method with an exception handler. Exception handler is a block of code that can officially handle the said exception. If the runtime system finds an appropriate handler (i.e. type of exception matches the type that can be handled), it will pass the exception object to the handler. This is called catching the exception. However, if the exception cannot be handled, the program will terminate. In Java, exceptions inherit from ‘Throwable class.’ NullPointerException and ArrayIndexOutOfBoundsException are two common exceptions in Java.

What is an Error?

An error is a condition that can be considered irrecoverable such as the program requiring an amount of memory larger than what is available. These errors cannot be handled at runtime. If an error occurs, the program will terminate. In Java, errors inherit from Throwable class. Errors usually stand for serious problems that the programmer (or the application) should not try to catch. Errors are simply abnormal conditions, which are never expected to occur under normal circumstances, and therefore never foreseen. For example, OutOfMemoryError, StackOverflowError and ThreadDead are such errors. Methods should never have handlers for errors.

What is the difference between Exception and Error?

Both errors and exceptions are unwanted occurrence during the execution of a program. However, they have key differences. Exceptions can be foreseen by the programmer, while an error is difficult to foresee. Exceptions can be checked or unchecked. But errors are always unchecked. Exceptions typically indicate an error caused by the programmer. However, errors occur due to a system error or an inappropriate usage of a resource. Therefore, exceptions should be handled at the application level, while errors should be taken care of at the system level (only if possible). After handling an exception, you are guaranteed to return to normal program flow. But even if an error is caught, the programmer may not know how to handle it in the first place. Unlike traditional error handling, exceptions allow separating error-handling code from regular code.