1 00:00:09,200 --> 00:00:09,770 Hi there. 2 00:00:09,800 --> 00:00:17,390 In this lesson, we are going to talk about error handling and to try and catch statement when we are 3 00:00:17,390 --> 00:00:24,800 developing software or web applications, we need to make sure that we handle the errors, avoiding 4 00:00:24,800 --> 00:00:26,700 our program from breaking. 5 00:00:27,110 --> 00:00:34,730 So let's get started with an example from the previous lesson before we had that callback function, 6 00:00:34,730 --> 00:00:42,050 when we added the time out and then we tried to use this user in the great user function. 7 00:00:42,440 --> 00:00:44,930 Let's remember that we got an error. 8 00:00:45,110 --> 00:00:48,200 So I'm just going to save this and refresh the page. 9 00:00:48,500 --> 00:00:51,400 And now, as you can see, we're getting an error. 10 00:00:52,160 --> 00:00:59,090 And when we get an error like this, let's see what happens when I write more code below it. 11 00:00:59,330 --> 00:01:01,460 So I'm just going to write a message here. 12 00:01:04,990 --> 00:01:08,590 Just going to write a new line of code, let's save this. 13 00:01:08,920 --> 00:01:10,000 Let's go back there. 14 00:01:10,240 --> 00:01:12,370 I'm going to refresh the page. 15 00:01:12,640 --> 00:01:13,780 And as you can see. 16 00:01:15,050 --> 00:01:23,030 Nothing that we write after the error is executed, and this means that when this happens, all the 17 00:01:23,030 --> 00:01:26,160 functionality of our page can be compromised. 18 00:01:26,300 --> 00:01:28,340 So how do we tackle this? 19 00:01:29,040 --> 00:01:33,820 How do we protect our application from breaking when we have errors? 20 00:01:34,100 --> 00:01:38,390 For that, we are going to use the try and catch statements. 21 00:01:40,530 --> 00:01:48,930 So going back there, instead of just trying to use this information, I'm going to start a try block. 22 00:01:50,750 --> 00:01:51,590 Like this. 23 00:01:53,260 --> 00:02:00,220 And I'm going to place this inside the tri block, so here we were simulating as if we were getting 24 00:02:00,220 --> 00:02:02,920 this information from an external source. 25 00:02:03,250 --> 00:02:08,380 So we are not sure if we are going to get this information right. 26 00:02:08,410 --> 00:02:14,150 We are not sure what's going to happen because this information is coming from outside in this simulation. 27 00:02:14,200 --> 00:02:18,480 So when we work with things like this, we should always use a try. 28 00:02:18,520 --> 00:02:20,890 So here we are going to try to do this. 29 00:02:20,890 --> 00:02:25,360 And after the try, we're going to add a catch block. 30 00:02:25,990 --> 00:02:29,620 And here we are going to pass an argument for the error. 31 00:02:29,860 --> 00:02:31,870 I'm going to talk about it in a moment. 32 00:02:32,170 --> 00:02:33,250 And inside here. 33 00:02:35,190 --> 00:02:42,660 Let's write another message, so this time let's just write high guest user. 34 00:02:52,130 --> 00:02:52,830 How are you? 35 00:02:53,510 --> 00:02:54,770 So what's going on here? 36 00:02:55,160 --> 00:03:02,870 I'm trying to get this information and sending this message, using that information. 37 00:03:05,170 --> 00:03:07,120 So the way this works is. 38 00:03:08,280 --> 00:03:15,060 We're going to go inside the try block, and if we can do everything without any error, we are just 39 00:03:15,060 --> 00:03:16,070 going to stop here. 40 00:03:16,290 --> 00:03:21,580 So if we can get the user and then call this function and there's no error, fine. 41 00:03:21,630 --> 00:03:22,970 We are just going to stop here. 42 00:03:23,220 --> 00:03:29,910 However, if there is an error for some reason, instead of breaking our program, we are going to jump 43 00:03:30,150 --> 00:03:32,690 directly into the catch block. 44 00:03:32,970 --> 00:03:37,590 So if an error occurs, it means that we were not able to get the user. 45 00:03:37,600 --> 00:03:42,000 So let's not send the message using that information. 46 00:03:42,010 --> 00:03:43,560 Let's just send a. 47 00:03:45,270 --> 00:03:46,950 Generic message. 48 00:03:47,880 --> 00:03:50,990 So now let's save this and let's see the difference. 49 00:03:52,430 --> 00:03:58,460 I'm going to refresh the page, so now there was an error with the message that we wanted to send, 50 00:03:58,580 --> 00:04:03,260 but instead of breaking the code, we just jumped to the catch block. 51 00:04:03,560 --> 00:04:09,380 Now we were able to send a generic message and everything. 52 00:04:09,380 --> 00:04:15,530 We right after that is just going to be executed normally, which is exactly how it was supposed to 53 00:04:15,530 --> 00:04:16,050 work. 54 00:04:16,130 --> 00:04:19,820 So now we are protecting our application from breaking. 55 00:04:20,060 --> 00:04:25,150 Now let's go back to Visual Studio Code and talk about this argument that we passed. 56 00:04:25,430 --> 00:04:32,570 This is actually the error message because using try and catch statements is really good to avoid the 57 00:04:32,570 --> 00:04:34,040 application from breaking. 58 00:04:34,040 --> 00:04:39,260 But if we are debugging it, if we want to know why we are getting an error. 59 00:04:40,360 --> 00:04:46,570 It's really hard because we don't have anything in the console, so bypassing this argument, let's 60 00:04:46,570 --> 00:04:48,250 do a console that log. 61 00:04:50,050 --> 00:04:57,340 And pass this this name can be any name you want, but this is a convention, so I suggest you always 62 00:04:57,340 --> 00:05:03,200 use this because every programmer that sees this will know that this is the error message. 63 00:05:03,490 --> 00:05:05,920 So if we do this, let's save this. 64 00:05:06,670 --> 00:05:07,930 Refresh the page. 65 00:05:10,350 --> 00:05:17,160 Now, as you can see, we are able to see what happened still our application did not break, but we 66 00:05:17,160 --> 00:05:18,900 were able to get that message. 67 00:05:19,380 --> 00:05:22,590 And if we were debugging this to understand. 68 00:05:23,730 --> 00:05:28,860 Why we have an error, it would be way, way easier to fix this issue. 69 00:05:29,160 --> 00:05:32,150 Now let's talk about the finally block. 70 00:05:32,160 --> 00:05:39,720 So going back to Visual Studio Code, besides using try and catch, we can also use a finally block. 71 00:05:41,060 --> 00:05:42,500 So this is optional. 72 00:05:42,530 --> 00:05:50,600 You don't need to use it if you don't want, the only statements that are mandatory are to try and catch 73 00:05:50,600 --> 00:05:53,930 statements you cannot use to try without the catch. 74 00:05:54,140 --> 00:05:58,850 You always need to use both, but you also have the finally, which is optional. 75 00:05:58,850 --> 00:06:04,010 So let's try to put this message inside a finally block. 76 00:06:09,480 --> 00:06:10,980 Let's right inside. 77 00:06:12,170 --> 00:06:20,480 Finally, block, so let's save this, refresh the page, actually, let me go back there just to comment 78 00:06:20,480 --> 00:06:24,020 this line so we don't get distracted by this. 79 00:06:26,240 --> 00:06:28,130 All right, refreshing the page. 80 00:06:30,320 --> 00:06:35,550 As you can see, this is a generic message, which means we had an error. 81 00:06:35,600 --> 00:06:41,720 We went to the catch block, but now that line of code is executed anyway. 82 00:06:43,640 --> 00:06:49,710 And now let's try removing the error, so this time we are not going to have a set time out. 83 00:06:52,160 --> 00:06:59,060 Which means that we won't have that error, so let's refresh the page and now, as you can see, this 84 00:06:59,060 --> 00:07:03,080 is not a generic message anymore, which means we didn't have an error. 85 00:07:03,590 --> 00:07:06,950 But we also have this line of code being executed. 86 00:07:08,760 --> 00:07:15,420 So this is how we use the final block, this is a block of code that you can put here to be executed 87 00:07:15,420 --> 00:07:18,690 no matter if you have an error or not. 88 00:07:18,910 --> 00:07:24,900 I actually don't see the point of using the final block, because if you think about it, if you leave 89 00:07:24,900 --> 00:07:31,500 it outside, it's just the same thing of using the finally because this is going to be executed anyway. 90 00:07:31,500 --> 00:07:38,400 So I think this is more a matter of organizing your code if the things you are doing inside here are 91 00:07:38,400 --> 00:07:39,010 related. 92 00:07:39,060 --> 00:07:41,040 So this is how you use the final block. 93 00:07:41,040 --> 00:07:43,370 And as I said, this is optional. 94 00:07:43,380 --> 00:07:46,050 So if you don't want to use it, you don't have to. 95 00:07:46,950 --> 00:07:52,570 Now, before we finish with this subject, let's talk about the throe statement. 96 00:07:52,770 --> 00:07:57,060 So for this, I'm going to go up here and let's leave it like this. 97 00:07:58,200 --> 00:08:05,670 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 98 00:08:05,670 --> 00:08:06,050 name. 99 00:08:06,270 --> 00:08:07,650 I'm going to save this. 100 00:08:09,990 --> 00:08:12,840 And how refreshing the page, actually. 101 00:08:14,540 --> 00:08:16,750 I'm just going to comment this line as well. 102 00:08:17,890 --> 00:08:24,400 Let's just leave the try and catch blocks, so let's refresh the page and see what happens. 103 00:08:24,700 --> 00:08:30,250 Now, as you can see, there were no errors because after all, we had this information. 104 00:08:30,490 --> 00:08:33,610 We had a variable with an object. 105 00:08:33,880 --> 00:08:36,730 This object had a name property. 106 00:08:36,730 --> 00:08:38,560 So there were no errors. 107 00:08:38,560 --> 00:08:46,840 But now we have a weird message because even though we received a user, the name was blank. 108 00:08:47,560 --> 00:08:53,080 So it would be useful to create our own customized errors. 109 00:08:53,420 --> 00:08:56,530 And this is what we can do with the throe statement. 110 00:08:56,860 --> 00:08:58,360 So now let's go back here. 111 00:09:00,280 --> 00:09:07,990 So first, let's get the user and now we know that any JavaScript error is going to make this go directly 112 00:09:07,990 --> 00:09:12,930 to the catch block, but we can also create our own errors. 113 00:09:13,150 --> 00:09:19,720 So if the username is empty, which we could do it like this. 114 00:09:19,750 --> 00:09:22,180 So if user not name. 115 00:09:23,430 --> 00:09:32,580 Is the same as an empty string, we could write it like this or we could just use the exclamation mark, 116 00:09:32,580 --> 00:09:39,450 which is the same thing if there's no username, which means that if it's zero or if it's an empty string, 117 00:09:39,450 --> 00:09:42,410 we are going to consider this as an error. 118 00:09:42,450 --> 00:09:44,700 So we are going to throw an error. 119 00:09:47,320 --> 00:09:55,240 And we can right any message we want, so in this case, we're going to write name is empty. 120 00:09:58,960 --> 00:10:05,770 So what's going to happen here inside the tri statement, besides looking for any error, every time 121 00:10:05,770 --> 00:10:11,970 we hit a throw statement, we are also going to be sent to the catch block. 122 00:10:12,280 --> 00:10:19,210 So when the interpreter hits this, it's just going to stop everything inside here and go directly to 123 00:10:19,210 --> 00:10:20,410 the catch block. 124 00:10:20,950 --> 00:10:22,540 So now let's try again. 125 00:10:25,180 --> 00:10:32,500 Refreshing the page, as you can see, we still have no error here, but instead of just using that 126 00:10:32,500 --> 00:10:36,780 empty string, we consider that as being an error. 127 00:10:36,790 --> 00:10:41,650 And now we are going directly to the catch block, which is really good. 128 00:10:41,920 --> 00:10:44,680 And why did we write this? 129 00:10:45,130 --> 00:10:47,260 Well, this is the error message. 130 00:10:47,260 --> 00:10:53,620 So if you are debugging your application and you want to understand what happened here, why are you 131 00:10:53,620 --> 00:10:55,270 not sending a message? 132 00:10:56,650 --> 00:10:59,470 If we send this error to the console. 133 00:11:03,380 --> 00:11:10,370 You can see that the string that we pass in the troll statement is going to be the error message in 134 00:11:10,370 --> 00:11:11,440 case that happens. 135 00:11:11,690 --> 00:11:14,270 So in this case, the name was empty. 136 00:11:14,480 --> 00:11:16,990 So we just created an error. 137 00:11:17,240 --> 00:11:22,190 And this is the error message which makes this go directly to the catch block. 138 00:11:22,370 --> 00:11:26,670 And the message we wrote is going to be passed as the error message. 139 00:11:26,870 --> 00:11:33,350 So this is how we use try and catch blocks to avoid our application from break. 140 00:11:33,590 --> 00:11:34,900 So that was it for now. 141 00:11:35,060 --> 00:11:36,590 I'll see you in the next video.