Welcome back.
In this video we're going to look at how to manage the different exceptions by themselves or individually.
So in our example that we had a few videos back, we asked the user to enter a number.
And when the user enters a number or is asked to enter a number into something that is called a string,
then something can go wrong, right?
A few things could go wrong.
You can see the exceptions that you get here.
Argument null exception.
Format exception.
Overflow.
Exception.
So if you hover over this parse method, it's pretty cool because it already shows you which kind of
exceptions can happen in that particular case.
So for the right line you can hover over it.
You can see it gives us the I o exception.
So the input output exception.
So there are so many different types of exceptions that we can have.
And each of them can be managed differently.
So if you let's say have an argument exception then you want to do a.
But if you have let's say a divided by zero exception, you can do B.
So let's have a look at how we can manage the different exceptions.
So we have this default exception which is called exception.
But then exception is a class.
And classes can have other classes that inherit from a class.
So we're going to see what inheritance is in depth in I believe the next chapter or maybe the chapter
after.
But for now, let's just take the divide by zero exception.
So we see here format exception overflow exception argument null exception.
Well actually this year this calculation can give us the divide by zero exception.
So I can catch that by saying divide by zero exception.
And I give it a name I call it x for example.
And then I can just say what should happen if that particular exception happens.
So if that arises well I'm just going to say Console.WriteLine.
And then the error message.
So x dot message.
So I can obviously also just say don't divide by zero colon or exclamation mark exclamation mark.
And then whatever the exception message will be.
So let's run this real quick just to see.
So let me divide by zero and you see don't divide by zero.
And then it says attempted to divide by zero.
So now it handled our exception differently.
Now let's run it again.
But this time we're not going to cause the divide by zero exception.
We're going to cause another exception where the number is not a number but an actual letter.
Okay.
So it says error.
The input string F was not in a correct format.
So instead what we can do is we can catch that exception as well.
So what was the exception that we got there?
I believe it was the format exception okay.
So if you want to know which exception it was you can give more details about the exception at this
point.
So instead of just displaying the message you could say two string okay.
So you can convert the exception into a string and then you would know more about it.
So here I'm going to use the format exception I'm going to call it x.
Now you can give it a different name each time.
You can call it however.
But in the end this is just a super mini local variable which will only be usable inside of this catch
block.
So now I'm going to use the same thing I'm going to say here, I told you to enter a number and then
we can just add the message as well here x dot message.
So now let's run it again and we will see if we enter F here.
I told you to enter a number.
The input string f was not in a correct format.
So you see you can now handle each exception differently.
And you can say that something different should happen depending on the exception.
Now in my case, I keep it simple.
I just write something onto the console.
Now, obviously you could treat it very differently by running other code that happens in such a case.
So so if for example, somebody divided by zero, you could say, okay, well you know what, I'm just
going to set the result to a default value for example.
So here I could just say on top of that result is equal to ten okay.
So if we divide by zero I'm just going to make result to ten.
So the error appeared.
And you can see the result is ten.
So this is just an example of what you can do.
Now obviously this is all depending on your use case.
So let's look at another exception that we can have.
So whenever we are working with integers we can make a very long integer.
So let's see.
So let's actually see how this is called I'm not sure myself to be honest.
So let's make it as two string here.
So let's convert it into a string.
Using the the brackets at the end.
Otherwise this won't work.
Now let me enter a super long number that will not fit into an INT and it says overflow exception as
I expected.
Okay, so the exception that we generated was an overflow exception.
So we the integer has only a certain amount of space.
And now it overflows.
Like basically you could say it's like a bucket that can only have a certain amount of numbers that
you can put into it.
But if you put more into it, then the bucket overflows.
So that's what we're achieving here.
So let's use this overflow exception.
You can see it's under system dot overflow exception.
So we can use exactly that.
We can catch that exception individually now.
And it's called overflow exception.
And here we're going to say number two high okay.
So let's run it again.
This time I'm not showing the error at all I'm just saying number two high.
And now let me enter a super long number.
And it says number two high.
There we are.
So this now gives you a general idea as to how you can handle different types of exceptions.
Now you can first of all force them using the default exception.
This will then give you details about the name of the exception.
And then you can catch that particular exception that arose.
Sometimes you don't manage all different types of exceptions, which is why it's very useful to still
have this exception, this default exception in there.
Okay.
So that's it for this video.
In the next video I want to show you what happens if you don't add this code here to your application.
So see you there.