An error usually refers to a condition that hinders a program’s normal execution. It can be due to issues.
An exception is a specific type of error that can be anticipated, caught and handled. It allows us to handle errors gracefully and does not break the program.
TL;DR:
If you’ve ever throw an error in a function expecting its invoker to catch it, you’re doing it wrong.
- Errors are unrecoverable, Exceptions are routin
- No matter what way you name things, you should handle Errors and Exceptions separately in your code or bad things happen.
- Most languages (Java, PHP) build the distinction into the language. Yet others (Go) name them the other way round. Some languages (JavaScript, Python) treat them as synonyms.
Error v/s Exception:
Errors:
Errors are serious issues that a typical application cannot handle. They are usually conditions that a program should not try to catch or handle, as they often indicate problems with the environment in which the program is running. Errors usually refer to issues that occur at runtime and are not typically recoverable. In many programming languages, errors are a subclass of exceptions.
Examples of errors:
- OutOfMemoryError: Thrown when the JVM cannot allocate an object because it is out of memory.
- StackOverflowError: Thrown when a stack overflow occurs because an application recurses too deeply.
- VirtualMachineError: Indicates that the JVM is broken or has run out of resources necessary for it to continue operating.
- Characteristics:
- Generally not intended to be caught or handled by the application.
- Indicate conditions that are outside the control of the application, such as hardware failures, JVM issues, or configuration problems.
- Usually, recovery from errors is not possible and may require restarting the application or system.
Exception:
- Definition: Exceptions are conditions that a reasonable application might want to catch and handle. They are usually related to application logic and can be foreseen and managed.
- Examples: IOException, SQLException, NullPointerException.
- Characteristics:
- Can be caught and handled by the application using try-catch blocks.
- Represent conditions that an application might be able to recover from or handle gracefully.
- Can be subclassed to create custom exceptions specific to the application.
Categories of Exceptions:
- Checked Exceptions:
- Must be either caught or declared in the method signature using the throws keyword.
- Represent conditions that a well-written application should anticipate and recover from.
- Examples: IOException, SQLException.
- Unchecked Exceptions (Runtime Exceptions):
- Do not need to be declared in a method’s throws clause and are not required to be caught.
- Indicate programming bugs, such as logic errors or improper use of an API.
- Examples: NullPointerException, ArrayIndexOutOfBoundsException.

Key Differences:
- Handling:
- Errors are usually not caught by the application.
- Exceptions are intended to be caught and handled by the application.
- Source:
- Errors typically stem from the environment in which the application is running.
- Exceptions typically result from the application’s code and logic
- Recoverability:
- Errors are often not recoverable.
- Exceptions can often be handled and allow the program to continue running.
Errors in JS:
In JavaScript, errors are represented by the Error object. There are several built-in error types, including:
- Error: A generic error.
- SyntaxError: Thrown when trying to interpret syntactically invalid code.
- ReferenceError: Thrown when referencing a variable that does not exist.
- TypeError: Thrown when a value is not of the expected type.
- Runtime Error: The divideByZero function checks for division by zero and throws an Error.
Here’s an example of each:

Conclusion:
Errors: Serious problems that an application generally should not try to catch. Examples include OutOfMemoryError and StackOverflowError.
Exceptions: Conditions that an application might want to catch and handle. They are divided into checked exceptions (e.g., IOException) and unchecked exceptions (e.g., NullPointerException).
This page was last edited on 29 July 2024, at 4:06 pm
How can we help you?























