‫Welcome back.
‫In this video, we're going to finally fix the problems that we had in our program here, our virtual
‫program that we created.
‫And we're going to also look into defensive programming.
‫So the idea of defensive programming is to make secure code that really already thinks about what problems
‫could arise, because in this case, the problems that could arise is that, for example, the list
‫is empty, or we want to have too many friends, so we want to invite more friends than we have.
‫And in this case, the list is a super simple one that we created manually.
‫But imagine it's from a database.
‫So it comes from a database or it comes from the web, from a cloud database or something like that.
‫And then it would be a super different story because we would never know how big the list is in this
‫case.
‫We can see it in the code, but we might not know it depending on where it comes from.
‫In this case, we have to anticipate different problems that could arise and we can test them ourselves
‫when developing.
‫For example, we already did that, right?
‫We entered more friends that we have in the list.
‫So it's a simple one.
‫We just run this and then we get this argument out of range exception.
‫But where we are getting it is an interesting one.
‫So we are getting it at the point of our get party different method even though the problem lies somewhere
‫else.
‫Because if we look at it, it says shortest name list zero.
‫So we're trying to access a list element that is not available because we are past the list, get party
‫friend and we pass this list over here.
‫So if we look at where we get this list from, that could be a good starting point because we can see
‫here we're trying to access something that is not available.
‫So what is the problem with what is made available?
‫So we can look at this line here, get party friend.
‫That's where it's called.
‫And we can see.
‫All right, that's the buffer.
‫Right?
‫So we get a buffer, which is this list over here.
‫So what we're getting is a list from the top level.
‫So again, we can see, all right, we get this list of so so-and-so, so many people, and this is
‫where we get the value from.
‫And well, a good point to check if the list is good is actually in our Get Party Friends method because
‫here we get the list, right.
‫So at this point we have a list so we can check this list.
‫We can see if this list is good enough for what we want to do.
‫And what we want to do is to write something based on the account of elements in that list so we can
‫check if the count is greater than list dot count.
‫And this is the list that was passed to us.
‫So if that's the case, what we can do is we could just print something, don't do that or error or
‫something like that.
‫But an even better thing that we can do is we can make the program crash.
‫So we know that the program has a problem, but at the same time, we stop the program from executing
‫at a very early stage because imagine we have a connection to a database and now we are creating all
‫of these different errors or we're just going through the code and this method get party friend is running
‫and let's say it's connected to database and it write something into the database and then we run the
‫code again.
‫And for some reason the database state has changed and now we're mindblowing because the data that is
‫displayed to us is complete nonsense and we cannot work with it.
‫So fixing that bug would be a real problem.
‫All right.
‫So we can already catch it as early as possible so we can simply throw an argument out of range exception
‫here.
‫So throw new argument out of range exception because that's what we were getting right.
‫And an argument out of exception has multiple overloads.
‫So we can have this one here with the param name.
‫We can have a message with the exception or a param name with a message.
‫So here I could just say something like, okay, the count is the problem and count cannot be greater
‫than elements in the list or something like that.
‫So that would be an example for what we could display.
‫So let's run it again because now we should be able to actually catch this error or at least throw an
‫argument out of range exception once we get this error.
‫All right.
‫So we can see exception on handled it says system argument out of range exception cannot be greater
‫than elements in the list parameter count.
‫So that's what is the result of what we have written here.
‫So this is how the exception will look like.
‫So now we know what the problem is.
‫It's a little more expressive.
‫Right before the problem wasn't actually here.
‫So let me comment those two lines out.
‫The problem wasn't thrown at this point, but it was thrown further down.
‫It was thrown in our get different method.
‫So it's in line 42.
‫But the actual problem arose a lot earlier.
‫Right.
‫And index was out of range exception.
‫That's also a very generic statement, right.
‫It could be anything.
‫Right.
‫So catching it earlier helps us to see where the problems are that we have.
‫So what we could, of course do is we could say, okay, once we enter this value and let's say this
‫value comes from a user that enters a value, right?
‫So you can enter any value from zero to the amount of list elements that we have.
‫So we could catch it even earlier.
‫We could do a test even here.
‫So if.
‫Friends.
‫Dot counts is.
‫Less than the value that we enter here, and we could get this value again from the user.
‫Then we could give them a message, right?
‫We wouldn't throw our exception.
‫We wouldn't make our program crash.
‫We would just ask the user to enter a new value.
‫But we're not doing it here because we as a programmer can enter the value here and we force these errors
‫manually right now.
‫Right.
‫So that's one way to solve it or actually one way to catch it early on.
‫Now, let's say.
‫We enter a stupid value here, such as minus one.
‫All right, now let's run it again.
‫Actually, let's run it again.
‫And our program seems to work, but it doesn't show anything there.
‫So it didn't show any.
‫Errors didn't throw any errors, but it doesn't show any friends.
‫Well, our program seems to work with minus one because we try to get minus one list elements from our
‫list and well, it doesn't give us any valuable information.
‫So now what we can, of course, do is we can add another condition here.
‫The count should be greater equals zero.
‫If that's not the case, then we could write something like called cannot be greater than the elements
‫in the list or lower zero.
‫So that's just one way of expressing it.
‫And now if we run it again, we will throw this error.
‫So that's something where the program didn't throw an arrow before, but now we as a developer understood,
‫okay, this is a state that we don't want to have.
‫This is a situation that we don't want to work with.
‫And as I said, we would cut well, we would try to get this value from somewhere and we would try to
‫catch it even before that.
‫Now, let's assume we have a situation which is even funnier.
‫So we have a situation where my friend's list is empty.
‫So I'm just going to create an empty list here.
‫Like, so now this will be commented out and now we have this empty list.
‫Let's run it again and see if our program still works.
‫And now we still get our argument out of range exception.
‫So that's pretty cool.
‫So it seems the exception that we created here fixes even this problem so we don't have to manually
‫look into what the list is like even though it's empty, right?
‫So if we look at our friends list, our friends list is empty, but in the end we get a count of zero.
‫So even though the list is null, the list is still zero.
‫So the value that we get there is still zero.
‫But that isn't all we can actually force an actual nul.
‫So instead of passing France with France which is this empty list, we can actually pass null here.
‫So let's say for some reason we couldn't create this list because the database connection didn't work
‫or something like that, and we get a null as a result and now we are passing null and we try to pass
‫null into a method that actually requires a list.
‫So let's run it and let's see what kind of error we get this time.
‫So now we get a null reference exception object reference not set to an instance of an object list was
‫null.
‫So the list that we passed now was now.
‫So we get this error even before we can throw an error.
‫So what we could do, of course, is we could catch this situation as well.
‫So we could go ahead and write another if statement.
‫If list is null, then throw a null pointer.
‫Exception throw new argument.
‫NULL exception.
‫So list.
‫The list is empty.
‫All right.
‫Of course, you can be more descriptive here instead of the list should not be empty.
‫Please check the Internet connection or something like that.
‫It could be the Internet connection or wherever you get your data from.
‫So let's run it again.
‫And now we should throw our argument now exception.
‫As you can see, the list is empty parameter list.
‫Perfect.
‫All right.
‫Now, you might wonder, why are we doing all of this?
‫I don't want my program to crash all the time.
‫Well, we do this in order to for our program to not get into the state where it would actually change
‫something in the database.
‫We're not changing anything in the database, in our little program.
‫But imagine it would be the case then.
‫It's a very different situation and we can create some serious problems here.
‫Now, I want to show you one final thing, because before we had all of this, right?
‫So before we had this if statement and this one and that one and so forth, and before we had this friends
‫list.
‫Yeah, I can actually stop.
‫Uh, before we had this friends here, and we passed friends over there, and then we try to have ten
‫friends at our party, but we only have seven friends.
‫So at this situation, once we run this, we can do is we can force this error that we had and the error
‫was in our get party friend.
‫So we get this argument out of range exception index was out of range.
‫So that's a problem that we had before we fixed it.
‫Right.
‫But now the interesting thing that we can look at is the call stack.
‫So there is something called call stack.
‫And let me make it a little bigger here.
‫Actually, let me close all those down.
‫So now let's look at this call stack.
‫This call stack here says that we are currently in line 46.
‫All right.
‫That's where we are.
‫So we are in the get party friend method.
‫Now, if we go one step higher, we can see that this is where to get party friend method was called.
‫Now we are inside of the get party friends plural method.
‫And what if you want to know where this method was called?
‫Well, it was called in line 13.
‫So with the call stack, we can see from where we were called, from where we were called again and
‫so forth.
‫So we can go up all the way to the level at which we what the initial method was called.
‫And the initial method was get party friends was called.
‫Then we go into this wild loop, then got get party friend is called, then we get into this method
‫here and then finally our variable is created based on a list that is empty.
‫All right, so I hope this gave you a little introduction into debugging.
‫Of course, there is a lot more to know, but I don't want to bore you with all the details because
‫some of the things are useful, but many of them will not be required in your day to day life.
‫Because, I mean, in general, you can, of course, go ahead and go to debug and then check out all
‫of the different windows you can get here and you can look into the threads that are running and parallel
‫the processes and so forth.
‫But I think what you have seen now is really going to be the main things or are going to be the main
‫things that you're going to require, the main tools that you're going to require in order to debug
‫your software.
‫All right.
‫So I hope you enjoyed this and I hope you can make use of this in your future programs.
‫And now you can finally see how debugging is solved.
‫And by the way, thanks for giving this feedback because I got the feedback that a debugging section
‫was missing, so I decided to create this little section.
‫So thanks a lot for the feedback.
‫If you have anything that you think is missing in this course or if you think that something is very
‫bad, please don't give me a bad rating directly, but let me know what it is so I can improve the course
‫because my goal is to make this the best C-sharp course on the market.
‫So thanks a lot for your help and I hope to see you in the next video.