Error Handling in JavaScript: Try, Catch, Finally
Why Errors are there and how to handle them in Javascript

While writing JavaScript, you will definitely run into situations where something breaks at runtime. It might be accessing a property that does not exist, parsing invalid data, or calling something that is not a function. These are called errors, and if not handled properly, they can stop your entire program from executing further. That is why error handling becomes important, not just to prevent crashes, but also to make your application behave in a controlled way even when something goes wrong.
Let’s start with a very basic runtime error so that the problem becomes clear.
console.log("Start");
let user = undefined;
console.log(user.name);
console.log("End");
Here, the moment JavaScript tries to access user.name, it throws an error because user is undefined. Because of this, the program stops immediately and "End" never gets printed. This is what happens when errors are not handled, execution breaks in between.
What errors are in JavaScript
Errors are situations where the code cannot execute as expected. These are usually runtime issues, meaning they occur when the code is running, not when it is written. JavaScript throws these errors automatically when it encounters something invalid, like accessing properties on undefined, calling something that is not a function, or invalid operations.
If we do not handle these errors, they bubble up and stop execution, which is not ideal in most real applications.
Using try and catch blocks
To handle such situations, JavaScript provides try and catch. The idea is simple, we try to execute a block of code, and if any error occurs inside it, we catch it and handle it.
console.log("Start");
try {
let user = undefined;
console.log(user.name);
} catch (error) {
console.log("Something went wrong");
}
console.log("End");
Now instead of breaking the program, the error is caught, and the program continues executing. So "End" will be printed.
This is called graceful failure. Instead of crashing, we handle the issue and move forward.
The finally block
There are situations where we want some code to run no matter what, whether an error occurs or not. That is where finally comes in.
try {
console.log("Inside try");
} catch (error) {
console.log("Inside catch");
} finally {
console.log("This always runs");
}
The finally block always executes. Even if there is no error, or even if there is an error and it is caught, finally will still run. This is useful for cleanup tasks or closing resources.
Throwing custom errors
JavaScript also allows us to create our own errors using throw. This is useful when we want to enforce certain conditions manually.
function withdraw(amount) {
if (amount > 1000) {
throw new Error("Limit exceeded");
}
console.log("Withdrawal successful");
}
try {
withdraw(2000);
} catch (error) {
console.log(error.message);
}
Here, we are manually throwing an error when a condition is not met. This helps in controlling the flow and making sure invalid operations are not silently ignored.
Why error handling matters
Error handling is important because it prevents your application from crashing unexpectedly. It allows you to show proper messages, log issues for debugging, and keep the rest of the application running.
Without error handling, even a small issue can break the entire flow. With proper handling, you can isolate the problem and deal with it in a controlled way.
It also helps a lot during debugging. Instead of getting random crashes, you get clear points where things failed, which makes it easier to fix issues.
Final clarity
Errors are a natural part of programming, especially in JavaScript where many things happen at runtime. Try and catch help in handling these errors gracefully, finally ensures that certain code always runs, and throw allows us to create our own errors when needed.
So instead of letting your program break, you take control of how it behaves when something goes wrong, and that is the main idea behind error handling in JavaScript.



