Welcome back.
In this video we're going to look at the throw keyword.
Now you might wonder what does throw do.
Well the throw keyword in C sharp is used to indicate that a problem has occurred in your program.
So when you use throw you're essentially saying, hey something went wrong here and I can't handle it
by myself.
You're creating an error on purpose, which is called an exception.
We're going to see how somebody else, so to speak, can handle the error later on.
So why would you use throw?
Well, you use it to stop bad things from happening.
If something in your program, for example, isn't right, like if a necessary file is missing or a
number that should never be zero is zero.
Using throw stops the program before any more damage can happen.
And then the second part is to tell other parts of your program about the problem.
So sometimes one part of your program might not know how to fix a problem, but another part does.
So by using throw.
The first part can let the second part handle the issue, and we're going to see that in a bit.
So essentially you could say that the throw keyword makes sure that nothing down the line breaks your
program even worse or causes irreparable damage later on.
So let's say if you don't throw this error right now, data that shouldn't go into the database will
go into the database and might screw up the entire database, or a user might get access to admin rights,
even though he should not get access to the admin rights, for example, right, or to the database
in general.
So if something like that happens, you can avoid it by using the throw keyword.
Or in general you can just throw an error.
You can just say, okay, well if this happens, I don't want the program to keep going, just crash
the app.
That's what we're saying here.
So let's have a look at what this could look like.
So I have this int get user h and we enter an h.
And then this h will be returned.
So what we're doing is we're getting the user input.
We're trying to parse it.
And we're saying give me the input.
And we are giving the H out.
So if it works out that whatever string was input that could be converted into a whole number, then
we're going to save that inside of H.
That's what this out H does.
And this exclamation mark just says, okay, if that didn't work, if that try parse is returning false,
then we're going to say throw new exception.
You didn't enter a valid H.
So if the H wasn't correct, well then we're going to do this on top of that.
In the next step we're going to say if the h is less than zero or higher than 120, we're also going
to throw an exception.
We're going to say your h must be between 0 and 120.
Now you could obviously in this particular example, which is very, very simple, it's really just
about showing you how the throw keyword works.
It's not about a logic that we would implement in a real world application.
We wouldn't crash our application because a user didn't enter the right number, or so we would want
obviously to collect or catch that, or use an if statement accordingly, or use a try catch for that.
But in this case, what I want you to achieve is really show you how this throw keyword works and how
it actually causes an exception and crashes the app.
So let's now use this.
By the way, we return the H after that.
So now let's use this method here inside of our main method.
And we're going to get console or use console dot read line to get the user input and pass that to our
get user h.
Now in order to be able to use this method, I need to make it static because we are inside of our static
program class.
Otherwise this wouldn't work.
So let the user also know that we want to have their H enter your age and then let's run our application.
So enter your age.
I'm going to enter five.
All is good.
Our application works flawlessly.
Now let's run it again.
And this time I'm going to enter 125.
And you see I run into this age error where it says system dot exception.
Your age must be between 0 and 120.
Okay.
So it said that this worked out so it could parse whatever I entered into a number.
That was cool.
But then it checked is the age between 0 and 120?
Well, it wasn't so bad for you or bad luck.
The application is going to crash.
Now let's cause the other crash.
Let's enter something that is not a number.
And you see, we get this other exception.
So our application is crashing here.
It says you didn't enter a valid age.
So now a very simple example would really be to say okay if the user enters a.
Ron Lungberg.
We crashed the app so that the wrong number, wrong age will never get to the database or the app just
can't work.
If this isn't correctly entered, for example, then we're just going to say hey, sorry, crashing
the app.
Not cool, but that's what it is.
All right.
But as I said, this is really only going to make sense if whatever happens with the wrong data or whatever
is going to cause major problems later down the line, like breaking your database or like giving a
user access to the admin rights or to the database general or something like that.
Okay, so these were just a few examples.
Examples.
Now you see how you can use the throw keyword to actively cause an exception.
Now in the next videos we're going to look at exceptions the different types that there are.
We have seen a few exceptions already, but we didn't see how we can manage them by themselves.
So separately so to speak, depending on which exception was caught.
So see you in the next video.