DEV Community

Cover image for Expecting the Unexpected: Javascript Throw & Catch
Ariel Davis
Ariel Davis

Posted on

Expecting the Unexpected: Javascript Throw & Catch

Error Handling

Error's are and will always be a big part of a developers life. They are what allow us to see what mistakes we made in out code, and learn more and more about what's going on behind the scenes. Sometimes however, we expect to get back and error from the code we've written. We need a way to handle this situation. This is where Error Handling comes into play.

Flatiron School

I am Ariel Davis, a current student at Flatiron School. I've come across error handling without going into too much detail a lot throughout my time at this school. After sitting down to really get an understanding of when to use error handling has been of great use to me and I can only hope it will be of great use to you as well. In this blog, I will go over when, why, and how, to use error handling in Javascript.

When?

When is it a good time to use error handling? There will be many times while programming when you come across an error. You shouldn't have the mindset of, "Oh there's an error, let me use error handling". Rather you should think like, "I know there are set conditions that would make this throw an error, so let me handle it." You should only use error handling when an error is expected to happen based off of certain conditions.

Why?

Why we should use error handling ties close to when we should use them. When your program throws an error that you are expecting, it will more than likely crash and burn. We don't want this to happen while our application is being used so we throw and catch an error.

How?

The bread and butter of this blog. How exactly do we work with error handling? In Javascript we use what's called the try...catch syntax:

try {

//Some code that may throw an error

} catch (error) {

//Some code that happens if/when an error occurs

}
Enter fullscreen mode Exit fullscreen mode

The code in try{..} is what is initially ran in your program.
Depending on if this piece of code throws an error or not, the next block of code catch(error) {...} is ran.
For example:

try {

console.log("Super cool console.log")

} catch (error) {

console.log("This console log won't run")

}
Enter fullscreen mode Exit fullscreen mode

However:

try {

console.log(undefinedVar)

} catch (error) {

console.log("This console log will now run")

}
Enter fullscreen mode Exit fullscreen mode

Because the variable undefinedVar isn't defined, an error is thrown. Now normally this would end the program, but with the error handling in place, the program will continue and run the console.log:
console.log("This console log will now run").

The Basics

That is the basic concept of error handling, some nice resources that can be used in order to get a different explanation of the try...catch syntax can be found at these links:
Try and Catch Article
MDN Docs
W3Schools

Top comments (1)

Collapse
 
laarchambault profile image
laarchambault

Wonderful! thanks for helping break down these important concepts.