Hi there.
In this lesson, we are going to talk about error handling and to try and catch statement when we are
developing software or web applications, we need to make sure that we handle the errors, avoiding
our program from breaking.
So let's get started with an example from the previous lesson before we had that callback function,
when we added the time out and then we tried to use this user in the great user function.
Let's remember that we got an error.
So I'm just going to save this and refresh the page.
And now, as you can see, we're getting an error.
And when we get an error like this, let's see what happens when I write more code below it.
So I'm just going to write a message here.
Just going to write a new line of code, let's save this.
Let's go back there.
I'm going to refresh the page.
And as you can see.
Nothing that we write after the error is executed, and this means that when this happens, all the
functionality of our page can be compromised.
So how do we tackle this?
How do we protect our application from breaking when we have errors?
For that, we are going to use the try and catch statements.
So going back there, instead of just trying to use this information, I'm going to start a try block.
Like this.
And I'm going to place this inside the tri block, so here we were simulating as if we were getting
this information from an external source.
So we are not sure if we are going to get this information right.
We are not sure what's going to happen because this information is coming from outside in this simulation.
So when we work with things like this, we should always use a try.
So here we are going to try to do this.
And after the try, we're going to add a catch block.
And here we are going to pass an argument for the error.
I'm going to talk about it in a moment.
And inside here.
Let's write another message, so this time let's just write high guest user.
How are you?
So what's going on here?
I'm trying to get this information and sending this message, using that information.
So the way this works is.
We're going to go inside the try block, and if we can do everything without any error, we are just
going to stop here.
So if we can get the user and then call this function and there's no error, fine.
We are just going to stop here.
However, if there is an error for some reason, instead of breaking our program, we are going to jump
directly into the catch block.
So if an error occurs, it means that we were not able to get the user.
So let's not send the message using that information.
Let's just send a.
Generic message.
So now let's save this and let's see the difference.
I'm going to refresh the page, so now there was an error with the message that we wanted to send,
but instead of breaking the code, we just jumped to the catch block.
Now we were able to send a generic message and everything.
We right after that is just going to be executed normally, which is exactly how it was supposed to
work.
So now we are protecting our application from breaking.
Now let's go back to Visual Studio Code and talk about this argument that we passed.
This is actually the error message because using try and catch statements is really good to avoid the
application from breaking.
But if we are debugging it, if we want to know why we are getting an error.
It's really hard because we don't have anything in the console, so bypassing this argument, let's
do a console that log.
And pass this this name can be any name you want, but this is a convention, so I suggest you always
use this because every programmer that sees this will know that this is the error message.
So if we do this, let's save this.
Refresh the page.
Now, as you can see, we are able to see what happened still our application did not break, but we
were able to get that message.
And if we were debugging this to understand.
Why we have an error, it would be way, way easier to fix this issue.
Now let's talk about the finally block.
So going back to Visual Studio Code, besides using try and catch, we can also use a finally block.
So this is optional.
You don't need to use it if you don't want, the only statements that are mandatory are to try and catch
statements you cannot use to try without the catch.
You always need to use both, but you also have the finally, which is optional.
So let's try to put this message inside a finally block.
Let's right inside.
Finally, block, so let's save this, refresh the page, actually, let me go back there just to comment
this line so we don't get distracted by this.
All right, refreshing the page.
As you can see, this is a generic message, which means we had an error.
We went to the catch block, but now that line of code is executed anyway.
And now let's try removing the error, so this time we are not going to have a set time out.
Which means that we won't have that error, so let's refresh the page and now, as you can see, this
is not a generic message anymore, which means we didn't have an error.
But we also have this line of code being executed.
So this is how we use the final block, this is a block of code that you can put here to be executed
no matter if you have an error or not.
I actually don't see the point of using the final block, because if you think about it, if you leave
it outside, it's just the same thing of using the finally because this is going to be executed anyway.
So I think this is more a matter of organizing your code if the things you are doing inside here are
related.
So this is how you use the final block.
And as I said, this is optional.
So if you don't want to use it, you don't have to.
Now, before we finish with this subject, let's talk about the throe statement.
So for this, I'm going to go up here and let's leave it like this.
So we won't have any error, but now let's let's leave it blank, so let's use an empty string for the
name.
I'm going to save this.
And how refreshing the page, actually.
I'm just going to comment this line as well.
Let's just leave the try and catch blocks, so let's refresh the page and see what happens.
Now, as you can see, there were no errors because after all, we had this information.
We had a variable with an object.
This object had a name property.
So there were no errors.
But now we have a weird message because even though we received a user, the name was blank.
So it would be useful to create our own customized errors.
And this is what we can do with the throe statement.
So now let's go back here.
So first, let's get the user and now we know that any JavaScript error is going to make this go directly
to the catch block, but we can also create our own errors.
So if the username is empty, which we could do it like this.
So if user not name.
Is the same as an empty string, we could write it like this or we could just use the exclamation mark,
which is the same thing if there's no username, which means that if it's zero or if it's an empty string,
we are going to consider this as an error.
So we are going to throw an error.
And we can right any message we want, so in this case, we're going to write name is empty.
So what's going to happen here inside the tri statement, besides looking for any error, every time
we hit a throw statement, we are also going to be sent to the catch block.
So when the interpreter hits this, it's just going to stop everything inside here and go directly to
the catch block.
So now let's try again.
Refreshing the page, as you can see, we still have no error here, but instead of just using that
empty string, we consider that as being an error.
And now we are going directly to the catch block, which is really good.
And why did we write this?
Well, this is the error message.
So if you are debugging your application and you want to understand what happened here, why are you
not sending a message?
If we send this error to the console.
You can see that the string that we pass in the troll statement is going to be the error message in
case that happens.
So in this case, the name was empty.
So we just created an error.
And this is the error message which makes this go directly to the catch block.
And the message we wrote is going to be passed as the error message.
So this is how we use try and catch blocks to avoid our application from break.
So that was it for now.
I'll see you in the next video.