Welcome back.
In this video we're going to look at try and catch really powerful keywords that allow us to catch exceptions,
which then in turn allow us to make sure that an error that usually would crash our application suddenly
doesn't crash our application, and it can still run as intended, or at least it can still run the
other stuff as intended.
So what would be a really good example where this is incredibly important?
Well, let's say you are downloading a file or you're storing data in a database or something like that,
and suddenly the connection breaks, then you don't want your application to crash.
And for that to happen so that it doesn't crash, you need to surround the code that could crash into
a try and catch.
So you need to make sure that if something goes wrong, that your application handles that problem accordingly.
So now let's look at how we can handle these errors accordingly so that our application doesn't crash
with a very simple example.
So we're not going to download data from a database yet or from a website or anything.
What we're going to do is we are just going to have this very simple example here where we're saying
int result equals zero.
So we have this integer.
Then we let the user know that they should enter a number, even though currently we're just going to
use a very simple number here.
So int num one is going to be zero.
And num two is going to be two.
And we're saying the result should be num two divided by num one.
Now you probably already see a problem here because what we're doing is we're dividing by zero.
And when you're dividing by zero you are causing an error.
So then we have this Console.WriteLine to display the result.
And we have this read key to leave our application running.
So let's run this code and you will see what I was talking about.
So we have this exception unhandled this unhandled exception that we ran into.
It says system divide by zero.
Exception attempted to divide by zero.
And this is something that is not good.
So our application crashed.
And if we look at our application says please enter a number.
But it didn't display the result.
So it didn't even get to line 16 where it would display the result.
So this is where we got the error in line 13.
And our application just crashes at this point.
So how can we avoid that?
How can we avoid it crashing?
Well obviously number one would be not to even create the problem at all, not divide by zero.
But another way to fix it would be to use try and catch.
So let me show you how try and catch works.
So you have this try keyword and then you have this catch keyword.
And then you use an exception.
And in this case I'm going to use x dot two string okay.
I'm just going to display the exception.
So I'm going to convert the exception into a string which then allows us to display it.
And let me actually display it by using console write line like so.
So now let's put all of this code here that could go wrong.
Let's put it into this try block.
So it's not outside of the try block anymore.
It's inside of the try block.
So what is going on here now.
Well we have this try block which allows us to run our code that could potentially go wrong.
Then we have this catch keyword where we also get the exception.
So we use this exception keyword.
Then we give it the name.
So we can use this exception inside of the catch block.
Now inside of these curly brackets.
So now it says console dot write line.
And then whatever exception happened display that to a string.
So let's run this code again now and see what's happening.
So we see it says please enter a number.
Well we don't enter a number yet but it says system divide by zero.
Exception attempted to divide by zero.
And then it says where it is at.
Try catch exception program main.
So that's the name of the exception right on my PC.
And in this file Program.cs in line 15.
That's where the error occurred.
But what's really interesting is you see result zero is still displayed.
So even though this error occurred and we are displaying the error, our code still kept going.
So you can see that there's no crashing, no nothing.
Our application works as intended.
So how can we make this better?
Well, first of all, instead of displaying the entire exception, we could just display a given message.
So the message is what is readable by the user.
So here it is attempted to divide by zero.
This is a lot more readable than what we had before.
We could even give a little more information.
We could say something like error, colon and then the message.
So now we know that we ran into an error.
It says error attempted to divide by zero.
So this is the example where we forced the divide by zero.
Now let's say instead of forcing and divide by.
So we're just trying to parse whatever the user entered into the console.
So let me run that again and it says please enter a number.
Let me enter five.
Everything works.
Now let me run this again.
Let me enter zero.
And you see attempted to divide by zero.
And then let me run this again and enter F.
And you see error.
The input string f was not in a correct format.
So this error obviously isn't perfect either, but at least it gives the user an idea as to what's going
on roughly.
And it's not exaggerating with the amount of details it gives you.
Still, our application kept on working and the result was still displayed.
So this is just an example as to what try and catch can do.
And we're using this exception.
Or we're getting this exception called X.
And we can use its details to display what the exception or error was about.
So if you use X here you can see that there are a few methods that are available but then also a few
properties.
So the message is one that we used.
You can also get the stack trace if you want to get more information.
And yeah these are the most useful ones I would say.
So this is our first look into try and catch.
So in the next video we're going to go deeper into it.
We still have another keyword in this try and catch context which is called finally.
So see you in the next video.