JavaScript basics error handling

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Thanks so much! I'm glad you like it ❤️
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

When it comes to writing code that is not breaking, we have to address error handling.
In JavaScript, this is managed through exceptions. But before we dive into those, let's look and see what happens when we introduce some faulty code.
iDontExist();
console.log('log me');
This will result in an error stating:
Uncaught ReferenceError: iDontExist is not defined
And stop our code completely.
Which is not ideal, because we don't want our code to crash right away.
The way to catch errors in JavaScript is to wrap them in a try...catch statement.
Let's do just that for the code we have above and see what happens.
try {
iDontExist();
} catch (error) {
console.error(error);
}
console.log('log me');
Now this will result in our error being logged, as well as our custom console.log being called.
To create a complete try...catch statement, we can introduce the finally block.
This block will be called once the whole code block is evaluated, and it doesn't care if it was an error or not.
try {
iDontExist();
} catch (error) {
console.error(error);
} finally {
console.log('fully done');
}
By now, we have seen how to catch errors, and often these are just the errors JavaScript throws for us.
But we can also introduce our own errors, because there might be some user error.
We can invoke these by using the throw method. This can then use an Error or a custom message.
try {
throw new Error('my error');
} catch (error) {
console.error(error);
} finally {
console.log('fully done');
}
Which will result in a custom error message saying my error.
try...catch blocksAnother cool thing to note is that we can even nest try...catch blocks.
The main thing to note when doing this is that the outer catch block will catch the exceptions.
try {
try {
throw new Error('my error');
} finally {
console.log('inner done');
}
} catch (error) {
console.error(error);
} finally {
console.log('fully done');
}
console.log('log me');
Resulting in the following flow:
// inner done
// Error: my error
// fully done
// log me
I hope you got a good understanding of error handling in JavaScript. If you have any question, don't hesitate to contact me.
You can also have a play with this on the following CodePen demo.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter