1 00:00:00,810 --> 00:00:01,770 ‫Welcome back. 2 00:00:01,800 --> 00:00:08,250 ‫In this video, we're going to finally fix the problems that we had in our program here, our virtual 3 00:00:08,250 --> 00:00:09,390 ‫program that we created. 4 00:00:09,390 --> 00:00:13,590 ‫And we're going to also look into defensive programming. 5 00:00:13,590 --> 00:00:23,220 ‫So the idea of defensive programming is to make secure code that really already thinks about what problems 6 00:00:23,220 --> 00:00:28,650 ‫could arise, because in this case, the problems that could arise is that, for example, the list 7 00:00:28,650 --> 00:00:34,800 ‫is empty, or we want to have too many friends, so we want to invite more friends than we have. 8 00:00:34,890 --> 00:00:38,970 ‫And in this case, the list is a super simple one that we created manually. 9 00:00:38,970 --> 00:00:41,370 ‫But imagine it's from a database. 10 00:00:41,370 --> 00:00:46,980 ‫So it comes from a database or it comes from the web, from a cloud database or something like that. 11 00:00:46,980 --> 00:00:52,740 ‫And then it would be a super different story because we would never know how big the list is in this 12 00:00:52,740 --> 00:00:52,980 ‫case. 13 00:00:52,980 --> 00:00:57,810 ‫We can see it in the code, but we might not know it depending on where it comes from. 14 00:00:58,990 --> 00:01:04,630 ‫In this case, we have to anticipate different problems that could arise and we can test them ourselves 15 00:01:04,630 --> 00:01:05,650 ‫when developing. 16 00:01:06,280 --> 00:01:08,520 ‫For example, we already did that, right? 17 00:01:08,530 --> 00:01:11,770 ‫We entered more friends that we have in the list. 18 00:01:11,770 --> 00:01:13,150 ‫So it's a simple one. 19 00:01:13,150 --> 00:01:18,760 ‫We just run this and then we get this argument out of range exception. 20 00:01:18,850 --> 00:01:21,250 ‫But where we are getting it is an interesting one. 21 00:01:21,250 --> 00:01:27,070 ‫So we are getting it at the point of our get party different method even though the problem lies somewhere 22 00:01:27,070 --> 00:01:27,640 ‫else. 23 00:01:27,640 --> 00:01:31,720 ‫Because if we look at it, it says shortest name list zero. 24 00:01:31,720 --> 00:01:39,250 ‫So we're trying to access a list element that is not available because we are past the list, get party 25 00:01:39,250 --> 00:01:41,920 ‫friend and we pass this list over here. 26 00:01:41,920 --> 00:01:49,060 ‫So if we look at where we get this list from, that could be a good starting point because we can see 27 00:01:49,060 --> 00:01:51,850 ‫here we're trying to access something that is not available. 28 00:01:51,850 --> 00:01:55,420 ‫So what is the problem with what is made available? 29 00:01:55,420 --> 00:01:57,970 ‫So we can look at this line here, get party friend. 30 00:01:57,970 --> 00:01:58,870 ‫That's where it's called. 31 00:01:58,870 --> 00:01:59,620 ‫And we can see. 32 00:01:59,620 --> 00:02:00,910 ‫All right, that's the buffer. 33 00:02:01,060 --> 00:02:01,480 ‫Right? 34 00:02:01,480 --> 00:02:05,890 ‫So we get a buffer, which is this list over here. 35 00:02:05,890 --> 00:02:10,120 ‫So what we're getting is a list from the top level. 36 00:02:10,180 --> 00:02:16,030 ‫So again, we can see, all right, we get this list of so so-and-so, so many people, and this is 37 00:02:16,030 --> 00:02:18,010 ‫where we get the value from. 38 00:02:18,010 --> 00:02:27,220 ‫And well, a good point to check if the list is good is actually in our Get Party Friends method because 39 00:02:27,220 --> 00:02:28,870 ‫here we get the list, right. 40 00:02:28,870 --> 00:02:31,810 ‫So at this point we have a list so we can check this list. 41 00:02:32,050 --> 00:02:35,530 ‫We can see if this list is good enough for what we want to do. 42 00:02:35,530 --> 00:02:41,560 ‫And what we want to do is to write something based on the account of elements in that list so we can 43 00:02:41,560 --> 00:02:48,910 ‫check if the count is greater than list dot count. 44 00:02:50,050 --> 00:02:52,780 ‫And this is the list that was passed to us. 45 00:02:52,780 --> 00:03:00,160 ‫So if that's the case, what we can do is we could just print something, don't do that or error or 46 00:03:00,160 --> 00:03:00,970 ‫something like that. 47 00:03:00,970 --> 00:03:05,950 ‫But an even better thing that we can do is we can make the program crash. 48 00:03:06,370 --> 00:03:14,230 ‫So we know that the program has a problem, but at the same time, we stop the program from executing 49 00:03:14,380 --> 00:03:21,280 ‫at a very early stage because imagine we have a connection to a database and now we are creating all 50 00:03:21,280 --> 00:03:27,760 ‫of these different errors or we're just going through the code and this method get party friend is running 51 00:03:27,760 --> 00:03:32,500 ‫and let's say it's connected to database and it write something into the database and then we run the 52 00:03:32,500 --> 00:03:33,490 ‫code again. 53 00:03:33,490 --> 00:03:39,910 ‫And for some reason the database state has changed and now we're mindblowing because the data that is 54 00:03:39,910 --> 00:03:43,600 ‫displayed to us is complete nonsense and we cannot work with it. 55 00:03:43,600 --> 00:03:46,390 ‫So fixing that bug would be a real problem. 56 00:03:46,600 --> 00:03:46,960 ‫All right. 57 00:03:46,960 --> 00:03:54,760 ‫So we can already catch it as early as possible so we can simply throw an argument out of range exception 58 00:03:54,760 --> 00:03:54,970 ‫here. 59 00:03:54,970 --> 00:04:01,540 ‫So throw new argument out of range exception because that's what we were getting right. 60 00:04:01,540 --> 00:04:04,870 ‫And an argument out of exception has multiple overloads. 61 00:04:04,870 --> 00:04:08,500 ‫So we can have this one here with the param name. 62 00:04:08,500 --> 00:04:13,450 ‫We can have a message with the exception or a param name with a message. 63 00:04:13,450 --> 00:04:23,620 ‫So here I could just say something like, okay, the count is the problem and count cannot be greater 64 00:04:24,580 --> 00:04:28,900 ‫than elements in the list or something like that. 65 00:04:29,890 --> 00:04:34,000 ‫So that would be an example for what we could display. 66 00:04:34,000 --> 00:04:41,830 ‫So let's run it again because now we should be able to actually catch this error or at least throw an 67 00:04:41,830 --> 00:04:44,470 ‫argument out of range exception once we get this error. 68 00:04:44,980 --> 00:04:45,310 ‫All right. 69 00:04:45,310 --> 00:04:51,160 ‫So we can see exception on handled it says system argument out of range exception cannot be greater 70 00:04:51,160 --> 00:04:54,760 ‫than elements in the list parameter count. 71 00:04:54,760 --> 00:04:58,330 ‫So that's what is the result of what we have written here. 72 00:04:58,570 --> 00:05:00,760 ‫So this is how the exception will look like. 73 00:05:00,760 --> 00:05:03,580 ‫So now we know what the problem is. 74 00:05:03,580 --> 00:05:06,280 ‫It's a little more expressive. 75 00:05:06,280 --> 00:05:09,280 ‫Right before the problem wasn't actually here. 76 00:05:09,280 --> 00:05:11,920 ‫So let me comment those two lines out. 77 00:05:12,400 --> 00:05:17,320 ‫The problem wasn't thrown at this point, but it was thrown further down. 78 00:05:18,570 --> 00:05:21,240 ‫It was thrown in our get different method. 79 00:05:21,240 --> 00:05:23,370 ‫So it's in line 42. 80 00:05:23,370 --> 00:05:26,010 ‫But the actual problem arose a lot earlier. 81 00:05:26,010 --> 00:05:26,310 ‫Right. 82 00:05:26,310 --> 00:05:28,620 ‫And index was out of range exception. 83 00:05:28,860 --> 00:05:31,020 ‫That's also a very generic statement, right. 84 00:05:31,020 --> 00:05:31,860 ‫It could be anything. 85 00:05:31,860 --> 00:05:32,370 ‫Right. 86 00:05:32,550 --> 00:05:39,240 ‫So catching it earlier helps us to see where the problems are that we have. 87 00:05:39,240 --> 00:05:45,720 ‫So what we could, of course do is we could say, okay, once we enter this value and let's say this 88 00:05:46,170 --> 00:05:51,390 ‫value comes from a user that enters a value, right? 89 00:05:51,390 --> 00:05:57,540 ‫So you can enter any value from zero to the amount of list elements that we have. 90 00:05:57,630 --> 00:05:59,880 ‫So we could catch it even earlier. 91 00:05:59,880 --> 00:06:01,680 ‫We could do a test even here. 92 00:06:01,680 --> 00:06:03,060 ‫So if. 93 00:06:03,750 --> 00:06:04,950 ‫Friends. 94 00:06:05,220 --> 00:06:08,130 ‫Dot counts is. 95 00:06:09,590 --> 00:06:15,150 ‫Less than the value that we enter here, and we could get this value again from the user. 96 00:06:15,170 --> 00:06:18,110 ‫Then we could give them a message, right? 97 00:06:18,110 --> 00:06:20,150 ‫We wouldn't throw our exception. 98 00:06:20,150 --> 00:06:21,980 ‫We wouldn't make our program crash. 99 00:06:22,010 --> 00:06:25,660 ‫We would just ask the user to enter a new value. 100 00:06:26,300 --> 00:06:32,870 ‫But we're not doing it here because we as a programmer can enter the value here and we force these errors 101 00:06:32,870 --> 00:06:33,650 ‫manually right now. 102 00:06:33,650 --> 00:06:34,130 ‫Right. 103 00:06:34,820 --> 00:06:40,700 ‫So that's one way to solve it or actually one way to catch it early on. 104 00:06:41,060 --> 00:06:42,260 ‫Now, let's say. 105 00:06:43,310 --> 00:06:46,760 ‫We enter a stupid value here, such as minus one. 106 00:06:47,720 --> 00:06:49,320 ‫All right, now let's run it again. 107 00:06:49,340 --> 00:06:52,310 ‫Actually, let's run it again. 108 00:06:55,650 --> 00:06:59,280 ‫And our program seems to work, but it doesn't show anything there. 109 00:06:59,280 --> 00:07:01,020 ‫So it didn't show any. 110 00:07:01,940 --> 00:07:05,570 ‫Errors didn't throw any errors, but it doesn't show any friends. 111 00:07:06,250 --> 00:07:12,280 ‫Well, our program seems to work with minus one because we try to get minus one list elements from our 112 00:07:12,280 --> 00:07:16,750 ‫list and well, it doesn't give us any valuable information. 113 00:07:17,230 --> 00:07:22,870 ‫So now what we can, of course, do is we can add another condition here. 114 00:07:22,990 --> 00:07:26,080 ‫The count should be greater equals zero. 115 00:07:26,410 --> 00:07:32,380 ‫If that's not the case, then we could write something like called cannot be greater than the elements 116 00:07:32,380 --> 00:07:35,470 ‫in the list or lower zero. 117 00:07:36,130 --> 00:07:39,040 ‫So that's just one way of expressing it. 118 00:07:39,040 --> 00:07:43,390 ‫And now if we run it again, we will throw this error. 119 00:07:45,060 --> 00:07:52,470 ‫So that's something where the program didn't throw an arrow before, but now we as a developer understood, 120 00:07:52,500 --> 00:07:54,780 ‫okay, this is a state that we don't want to have. 121 00:07:54,780 --> 00:07:57,900 ‫This is a situation that we don't want to work with. 122 00:07:58,530 --> 00:08:04,800 ‫And as I said, we would cut well, we would try to get this value from somewhere and we would try to 123 00:08:04,890 --> 00:08:06,570 ‫catch it even before that. 124 00:08:06,960 --> 00:08:10,890 ‫Now, let's assume we have a situation which is even funnier. 125 00:08:10,890 --> 00:08:17,370 ‫So we have a situation where my friend's list is empty. 126 00:08:17,370 --> 00:08:20,220 ‫So I'm just going to create an empty list here. 127 00:08:21,390 --> 00:08:28,050 ‫Like, so now this will be commented out and now we have this empty list. 128 00:08:28,050 --> 00:08:30,510 ‫Let's run it again and see if our program still works. 129 00:08:32,650 --> 00:08:36,460 ‫And now we still get our argument out of range exception. 130 00:08:36,460 --> 00:08:37,630 ‫So that's pretty cool. 131 00:08:37,630 --> 00:08:45,310 ‫So it seems the exception that we created here fixes even this problem so we don't have to manually 132 00:08:45,310 --> 00:08:49,900 ‫look into what the list is like even though it's empty, right? 133 00:08:49,900 --> 00:08:55,720 ‫So if we look at our friends list, our friends list is empty, but in the end we get a count of zero. 134 00:08:55,720 --> 00:09:00,370 ‫So even though the list is null, the list is still zero. 135 00:09:00,370 --> 00:09:02,860 ‫So the value that we get there is still zero. 136 00:09:04,500 --> 00:09:09,430 ‫But that isn't all we can actually force an actual nul. 137 00:09:09,450 --> 00:09:15,360 ‫So instead of passing France with France which is this empty list, we can actually pass null here. 138 00:09:16,170 --> 00:09:22,800 ‫So let's say for some reason we couldn't create this list because the database connection didn't work 139 00:09:22,800 --> 00:09:29,580 ‫or something like that, and we get a null as a result and now we are passing null and we try to pass 140 00:09:29,580 --> 00:09:33,030 ‫null into a method that actually requires a list. 141 00:09:33,030 --> 00:09:37,290 ‫So let's run it and let's see what kind of error we get this time. 142 00:09:38,440 --> 00:09:45,100 ‫So now we get a null reference exception object reference not set to an instance of an object list was 143 00:09:45,100 --> 00:09:45,490 ‫null. 144 00:09:45,490 --> 00:09:47,320 ‫So the list that we passed now was now. 145 00:09:47,320 --> 00:09:51,460 ‫So we get this error even before we can throw an error. 146 00:09:51,520 --> 00:09:56,800 ‫So what we could do, of course, is we could catch this situation as well. 147 00:09:56,830 --> 00:10:01,360 ‫So we could go ahead and write another if statement. 148 00:10:01,390 --> 00:10:07,570 ‫If list is null, then throw a null pointer. 149 00:10:07,570 --> 00:10:12,040 ‫Exception throw new argument. 150 00:10:12,880 --> 00:10:13,990 ‫NULL exception. 151 00:10:15,380 --> 00:10:16,820 ‫So list. 152 00:10:19,290 --> 00:10:21,450 ‫The list is empty. 153 00:10:22,140 --> 00:10:22,800 ‫All right. 154 00:10:23,640 --> 00:10:27,240 ‫Of course, you can be more descriptive here instead of the list should not be empty. 155 00:10:27,270 --> 00:10:30,300 ‫Please check the Internet connection or something like that. 156 00:10:30,310 --> 00:10:33,270 ‫It could be the Internet connection or wherever you get your data from. 157 00:10:35,560 --> 00:10:36,970 ‫So let's run it again. 158 00:10:36,970 --> 00:10:39,990 ‫And now we should throw our argument now exception. 159 00:10:40,000 --> 00:10:42,790 ‫As you can see, the list is empty parameter list. 160 00:10:43,390 --> 00:10:44,170 ‫Perfect. 161 00:10:45,400 --> 00:10:45,700 ‫All right. 162 00:10:45,700 --> 00:10:47,740 ‫Now, you might wonder, why are we doing all of this? 163 00:10:47,740 --> 00:10:49,830 ‫I don't want my program to crash all the time. 164 00:10:49,840 --> 00:10:58,180 ‫Well, we do this in order to for our program to not get into the state where it would actually change 165 00:10:58,180 --> 00:10:59,200 ‫something in the database. 166 00:10:59,200 --> 00:11:01,900 ‫We're not changing anything in the database, in our little program. 167 00:11:01,900 --> 00:11:04,090 ‫But imagine it would be the case then. 168 00:11:04,090 --> 00:11:07,960 ‫It's a very different situation and we can create some serious problems here. 169 00:11:08,110 --> 00:11:14,410 ‫Now, I want to show you one final thing, because before we had all of this, right? 170 00:11:14,410 --> 00:11:22,270 ‫So before we had this if statement and this one and that one and so forth, and before we had this friends 171 00:11:22,300 --> 00:11:23,200 ‫list. 172 00:11:26,670 --> 00:11:28,950 ‫Yeah, I can actually stop. 173 00:11:29,990 --> 00:11:37,140 ‫Uh, before we had this friends here, and we passed friends over there, and then we try to have ten 174 00:11:37,140 --> 00:11:39,600 ‫friends at our party, but we only have seven friends. 175 00:11:39,600 --> 00:11:49,260 ‫So at this situation, once we run this, we can do is we can force this error that we had and the error 176 00:11:49,260 --> 00:11:51,060 ‫was in our get party friend. 177 00:11:51,390 --> 00:11:56,340 ‫So we get this argument out of range exception index was out of range. 178 00:11:56,670 --> 00:11:58,950 ‫So that's a problem that we had before we fixed it. 179 00:11:58,950 --> 00:11:59,370 ‫Right. 180 00:11:59,370 --> 00:12:04,350 ‫But now the interesting thing that we can look at is the call stack. 181 00:12:04,350 --> 00:12:06,090 ‫So there is something called call stack. 182 00:12:06,300 --> 00:12:09,120 ‫And let me make it a little bigger here. 183 00:12:09,120 --> 00:12:11,490 ‫Actually, let me close all those down. 184 00:12:11,490 --> 00:12:13,920 ‫So now let's look at this call stack. 185 00:12:14,610 --> 00:12:19,560 ‫This call stack here says that we are currently in line 46. 186 00:12:19,560 --> 00:12:19,890 ‫All right. 187 00:12:19,890 --> 00:12:20,910 ‫That's where we are. 188 00:12:20,940 --> 00:12:23,370 ‫So we are in the get party friend method. 189 00:12:23,400 --> 00:12:29,910 ‫Now, if we go one step higher, we can see that this is where to get party friend method was called. 190 00:12:30,060 --> 00:12:34,620 ‫Now we are inside of the get party friends plural method. 191 00:12:35,130 --> 00:12:38,820 ‫And what if you want to know where this method was called? 192 00:12:38,820 --> 00:12:40,860 ‫Well, it was called in line 13. 193 00:12:40,860 --> 00:12:48,180 ‫So with the call stack, we can see from where we were called, from where we were called again and 194 00:12:48,180 --> 00:12:48,510 ‫so forth. 195 00:12:48,510 --> 00:12:55,530 ‫So we can go up all the way to the level at which we what the initial method was called. 196 00:12:55,950 --> 00:12:59,430 ‫And the initial method was get party friends was called. 197 00:12:59,430 --> 00:13:05,190 ‫Then we go into this wild loop, then got get party friend is called, then we get into this method 198 00:13:05,190 --> 00:13:10,920 ‫here and then finally our variable is created based on a list that is empty. 199 00:13:12,690 --> 00:13:17,700 ‫All right, so I hope this gave you a little introduction into debugging. 200 00:13:17,700 --> 00:13:22,260 ‫Of course, there is a lot more to know, but I don't want to bore you with all the details because 201 00:13:22,260 --> 00:13:28,530 ‫some of the things are useful, but many of them will not be required in your day to day life. 202 00:13:28,530 --> 00:13:36,150 ‫Because, I mean, in general, you can, of course, go ahead and go to debug and then check out all 203 00:13:36,150 --> 00:13:41,250 ‫of the different windows you can get here and you can look into the threads that are running and parallel 204 00:13:41,250 --> 00:13:43,860 ‫the processes and so forth. 205 00:13:43,860 --> 00:13:50,040 ‫But I think what you have seen now is really going to be the main things or are going to be the main 206 00:13:50,040 --> 00:13:54,510 ‫things that you're going to require, the main tools that you're going to require in order to debug 207 00:13:54,510 --> 00:13:55,500 ‫your software. 208 00:13:55,860 --> 00:13:56,190 ‫All right. 209 00:13:56,190 --> 00:14:01,350 ‫So I hope you enjoyed this and I hope you can make use of this in your future programs. 210 00:14:01,350 --> 00:14:04,590 ‫And now you can finally see how debugging is solved. 211 00:14:04,590 --> 00:14:10,920 ‫And by the way, thanks for giving this feedback because I got the feedback that a debugging section 212 00:14:10,920 --> 00:14:13,890 ‫was missing, so I decided to create this little section. 213 00:14:13,890 --> 00:14:15,660 ‫So thanks a lot for the feedback. 214 00:14:15,660 --> 00:14:20,100 ‫If you have anything that you think is missing in this course or if you think that something is very 215 00:14:20,100 --> 00:14:25,800 ‫bad, please don't give me a bad rating directly, but let me know what it is so I can improve the course 216 00:14:25,800 --> 00:14:28,920 ‫because my goal is to make this the best C-sharp course on the market. 217 00:14:28,920 --> 00:14:33,330 ‫So thanks a lot for your help and I hope to see you in the next video.