Welcome back.
In this video I want to show you how debugging and breakpoints work.
So there is this feature of breakpoints which allows us to stop our program execution or code execution
at a specific point.
So let's say instead of the num two always being 13, let's just use the console dot read line in order
to get the user input at this point.
Now we will however need to convert this into an int.
How do we do that?
Do you recall?
Well we could use int dot parse and then do the conversion of whatever we enter.
And maybe we tell the user also to write something.
So console dot write line I'm going to say something like please enter a whole number like so.
And this will then ask the user for some input.
And then what I want to do is I want to go through all of these calculations and so forth.
So what I can do is I can have a breakpoint just at this point here.
For example, this will pause the execution of my code just at this line here at line six.
So let's start with this play button here.
And you see it says please enter a whole number.
And now let's say I want to enter something in here.
I can't because my software is frozen at at this point.
So the code has not been executed past the end of line five.
So line six has not been entered yet.
So you see Num2 is by default set to zero.
That's because ints are by default set to zero, and I have not entered anything yet.
But still it already says that the number is zero.
So now if I want to actually enter something as a user, I can go over to the next point.
So the next line of code, by clicking on step Over.
Now this line will be executed.
And you see I've hammered away on the five and now it has well entered all of those numbers in here.
So let's say I enter five in here.
Then it goes to the next line of code and executes this here.
Because that's what I did with this debugging.
I'm basically going from one step to the next inside of my code, as there is no code here and this
is just a comment that is ignored.
This is also no code and another comment that is ignored.
This is the next line where actual code is executed.
So here I can now hover over Num2 and you see Num2 is now actually holding the number five.
That is because I just entered the number five into my console.
So it did not write this statement yet where it says H is num one.
Okay.
It's not at that point yet, which is something we can get over with by pressing continue.
And by pressing continue, it will just execute the rest of the code.
Okay.
So it will just go through all of the rest of the code and all of the other lines that are still there.
So it's going to say H 14.
It's going to do all of the calculation and do its job.
So now let me run this once again like so.
And here I am again at this breakpoint I cannot enter anything because my software isn't ready yet.
So I entered five twice just to test.
So now I'm going to step over again.
Now it shows me the five that I hammered.
Let's say I crash my software so I enter zero.
Okay.
Like so it goes to the next line.
Let me say.
Or let's say we go from one step to the next.
So I want to go to the next line by pressing this key here.
Step over or f ten is also something you can do.
So then it goes into the next line, and I can hover over the number one or the variable and see which
value it holds.
So this is really useful whenever you're working with a bunch of variables.
And you want to see what value they hold.
Now in our example it's quite simple because we can just look at the code.
But sometimes you get data from an external source or it's calculated somehow and it is a lot more complicated,
and suddenly it's not that easy to see which number it would be without debugging and using the approach
that you see right here.
So then if I step over one more time, you can see Num1 plus Num2 is 14.
That's because zero plus 14 is 14.
So this addition worked flawlessly.
And we're going to run into an error.
Maybe you know which error it will be.
So now let's do multiplication.
Still very simple.
We just get zero.
So 14 times zero is zero.
And now comes the interesting part.
So here we have 14 divided by zero okay.
So let's run it and we get an exception.
So we get an error.
And we now know exactly where the error arose.
It arose in line 21.
And it says system divide by zero.
Exception attempted to divide by zero, so it is impossible to divide by zero, and encoding it is forbidden
to divide by zero, which is why you get this exception, this error, so to speak, where it says divide
by zero exception.
So it could not execute that code and that will make our application crash.
All right.
So that's something I want to make you aware of where you can crash your app.
And you can even see well you can't see it now.
But I can't get back to my app because my app just closed because it crashed.
There was nothing to do anymore.
It just crashed.
It couldn't display the results because I forced it into an exception with the number that I entered
as a user.
So this shows you something.
You might write your software in the best possible way, but as soon as you have user input or external
data that you load from a server or something like that, you need to be a lot more careful with your
software, so you need to write it in a way that makes it error proof, so to speak.
And in this case, we didn't make it error proof because we just asked the user to enter something and
the user could have entered a non number.
For example, this would have crashed the app as well.
So we can run it again.
And let's step over the first step so that we get into the user entry field.
And I'm just going to say hi.
And this instantly crashes our application again where it says format exception this time.
So the input string hi was not in a correct format because it was expecting an int.
So or at least something that it can convert into an int so into a number okay.
So that is basically what I wanted to show you in this video.
There's one last little trick that I wanted to show you because we saw earlier this evaluation, this
order of evaluation.
And I wanted to show you actually here we will get a compiler error.
By the way, just quickly, the difference between a compiler error and a runtime error is what we just
saw.
So a compiler error is here.
This here is a compiler error.
I get an error while my program is trying to tell the IDE.
Visual studio is trying to compile the code, basically trying to pre run it for me, so to speak,
and it says, hey, here's an error, fix it.
The runtime error that we had was the error that happened during the runtime.
So the execution of our application while it was running.
So when I entered a zero, for example, and it tried to divide by zero, that's when I got a runtime
error.
Okay.
So there's this neat little trick.
And while I am in debug mode when pressing this play button, and I now want to see what this here will
be the result of this, including this little, um, quotation at the beginning.
And I can now press shift F9, and this will give me that.
The result of this expression will be a ditione num plus num two equals 14.
So this now is of type string.
It says.
So this 14 is the num one that had this default value.
So you see that by default at this point this is a string all the way up to here.
So when we're trying to add this which is an int to our string, this will be turned into a string as
well.
Which then leads us to using concatenation here.
So this is an example of concatenation.
So if we finalize running it and here I'm just going to step over and actually like continue not step
over but continue the whole application.
You see I get 145 instead of 14 plus five which would be 19.
So again this is how you can, uh, check inside of your application what a value at a given point is,
even though we're not putting it into a separate variable or whatever, because otherwise we could have
put this into a separate variable and then do all of the magic here.
But the cool thing is we could do that instantly, directly in the IDE while debugging.
Okay.
So that's it for this video.
See you in the next one.