This video is surrounding exception handling in C sharp.
I have already created the projects and we're calling this one exception handling so you can pause right
here.
Create the project and resume once you have done that.
No.
As an exception is a problem that arises during the execution of a program and it is not unique to C
sharp which is pretty much in every single programming language.
It's usually a response to you know invalid inputs are undesirable conditions for an operation so you
may have encountered maybe the ABM where they asked you to insert your selection and you press one when
you know you compress one but you press one and then if you see a big nasty arrow screen then that means
there exception handling was probably poor because they didn't factor in that this could potentially
happen.
Now when you're writing your application you are the one who is determining the inputs and the you know
the outputs and want more importantly you know the desired inputs.
So there are situations that you can foresee a potential error.
That's what exception handling is all about.
You're going to write your code in a what we call defensive manner so that you see that under these
circumstances going through a nasty error don't exit the program but should the user a nice error message
to say hey you know you did something wrong when we could have been a potentially fatal operation for
your program.
So I've added some notes to this code file a the try catch finally and throw keywords all of which are
keywords used when we're talking about exception handling mode to try.
Basically is just a block that says I'm going to try this operation.
I'm going to try and do this.
The catch basically says catch in the arrow like stop and check if there's any arrow.
After you try it.
If there is an arrow then I want to take some action.
The finally basically says that okay.
After we tried it and maybe something was got maybe something was not caught I'm still going to do this
operation anyway.
And then the thrill is a key word that we use to end the execution.
So we sometimes in programming you would try something and then you catch a particular kind of exception
and then you the programmer know that if this exception is ever caught that is going to be detrimental
to whatever operation is able to happen.
So you can manually throw all the the program out as in when you do throw it will literally throw the
arrow to the screen and exit the program.
All right.
So let's start this adventure with a small program I wrote.
It's already taken the spores underwrites it off yourself.
So we're just accepting two numbers.
We're doing a console right line.
Enter your first number and then I'm passing my integer in a different method.
So we would have done convert dot to int 32.
No I'm doing it another way where there is actually a focus on instead of the int data type called Dot
pass and then it works pretty much the same way except the converts to in 32 has a wider range than
a dot pass.
But for the purpose of this example we can just use the dot pass.
So we're asking the user for two numbers and then we're going to do some division and then we're going
to display the results.
No I'm going to run this program the first time and then we're going to see that 10 divided by two is
going to be five and that's perfect.
And we know that okay our application works.
Now you're probably saying okay that's straight forward that's thunder.
That's what was expected.
What is the moral of the story.
So let's execute this again and then this time I'm going to do something that I know is going to throw
an exception and so I'm going to see 10 divided by zero and anybody who knows math knows that there
is no division by zero no look at what visual studio does.
When I press enter it throws what we call an exception AC exception on the handle.
They're telling us that this video this lesson is called exception handling and they're telling us that
this exception was on a handled system.
Divide by zero exception attempt to divide by zeros at its end what the exception was.
So once again there are certain situations under which your program is just going to freak out because
those operations are illegal.
All right.
So even in programming even though we're telling the computer to do it the computer cannot divide by
zero.
So exception handling would allow us and so in it's in this state we have to just continue in Visual
Studio and then it will get back to the states where we can start writing code again.
I know we'll have to write a block that says let me handle the exception.
So to start off we try so I can just say a try and then open with fairly braces and then I'm going to
try and operation.
So I kind of soft try and empty because you know this is our visual studio is telling me that it's incomplete.
So what am I going to try.
I don't need to try that input because I know that they're going to put input inputs but I'm going to
try the operation which is the division right.
So I'm going to try to divide.
And then I am going to follow that up with a catch.
No there are many exceptions and we just saw a very specific one we saw one that says system divide
by zero exception you also have system that Io accepts and also a system that every time mismatch exception
you have a vote of memory except when you have a number of exceptions.
No I'm not telling you to remember all of them.
I'm just saying that there are a lot.
So they all come from one based class called exception.
All right.
So we just type exception we can literally just cover every base and then we're just going to say Exception
e.
So I'll explain what that is.
But when we'd say the keyword exception is like a base class that covers every type of exception so
much of the type of exception that is caught it will be caught by this class.
No you are probably wondering okay so why would I need to specify the type of exception.
Well there are times when based on the type of exception that is caught you want to do something different.
So like the divide by zero you probably want to from them to enter a better denominator or not denominator
advisor sorry.
And if it is maybe a system not I owe exception then you want to bring something else so you can actually
have multiple catch blocks so I'm just going to close the catch block so that's what the try catch looks
like.
So when people talk about taxing on handling or you hear somebody say try catch this is what they're
talking about.
So you try on Operation you catch the arrow and then you can actually catch many arrows.
I can have multiple catches and each catch would just have a specific exception so I can specify that
I want the division by zero exception to be caught in this catch block and then I can go on to see well
actually the ordering here matters.
So I'm sorry about that.
If I want to be specific then I have to do the specific ones before I catch exception.
Because then if I did catch exception before the divide by zero then catch exception would have been
catching any type of exception anyway and it would never have gotten to the divide by zero.
Right.
So that then the order much as well like I will see if we just have exception like visual studio just
indicated then we are catching all of the exceptions possible.
No I did salesman to explain the purpose of the EA so I had EA and then I said EA x or whatever so this
is pretty much just an object that represents the exception.
So you notice when the exception message came up earlier they had some details to a name it had the
content.
Told us what the arrow was.
So all of those components all of those bits of information we can all collect them and store it inside
of some variable or some object called E and then that will allow us to will print or whatever we want.
So let's say we tried this Operation All right.
And then after trying the operation we called the exception.
That said division by zero.
So when I catch that exception I can choose to console right line and print so it's an error message
illegal operation.
So instead of giving that nasty arrow where it stopped the operation and started telling us about its
system divide by zero decided that because that means really nothing to a user you can give a more elegant
error message to say Hey man you know illegal operation right.
So I'm going to try the operation then print the results and then I'm going to catch any arrow and then
I can see if finally all right console the right line.
So I just do a quick console right line and see I just copied and pasted that end of program so remember
that the finally will execute regardless of the trying to catch operation so it's going to try this.
If it's successful then it's going to do this finally if it tries it and there is an error that it catches
then it's going to still do this finally.
So let's try that again so we're going to do a good one.
Ten and two.
And then you see results and then end of program.
So it tried it and then it did finally.
Let's try that again so this time I'm doing 10 and 0.
And then you see illegal operation.
So give me my error message and then it still did.
Finally some way to kind of me the error message a bit more explicit.
No.
Someone to see illegal operation and put a call on.
And let me just use interpolation because I am going to be printing the actual error message from the
exception.
So exception is generic.
Once again I don't know if it's a divide by zero or a system that I or sometimes you get system that
Io exceptions they have 20 different arrows instead of that exception type.
All right so sometimes it is good to know the exact error that is being thrown so that you can be better
at debugging the application but sometimes it's not deliberate.
I'm being deliberate.
No but a lot of the times we write code and we're not being deliberate with the arrows and we're probably
missing something.
So I'm going to just say e dots and then I can see that message.
So e the message is the actual message attached to the exception so the expectation is that when the
exception is thrown or courts or other we're going to print illegal operation on call on and then the
actual error message.
So let's try that again so 10 and zero once more and then we're seeing here illegal operation cool on
and then they're giving us the message attached to the exception which is that we attempted to divide
by zero.
So that's a nice way to handle arrows.
And then once again the final will still execute.
And as it will do what it needs to do.
No we did say there's a keyword called throw and the throw and I'm went out to put this through above
this console the right line notice so it kind of becomes though and if I hover over it then it's going
to be telling me that it is unreachable seats is unreachable code detected because the fact is that
the throw is just going to kill the program right there.
All right.
So when I say try and then catch exception if I say throw then it just kills the operation.
There is no elegant error message.
Let's even test to see if there is even a final.
So I believe there is a finally.
But let's just validate that so 10 and zero once more and then you see it just go straight to the.
So it just stops the program comes to through and I mean visits to do is showing us what the problem
is but then if your console application is not executing inside of goes through then there is there
is none off the screen for the user.
All right so it would just exit the program and they would never know why it exited the program.
All right.
And there's the on hand of the exception something and this is the error message that would come up
for that user if we use through.
So once again exception handling is a bowl watching for particular types of errors and handling them
elegantly.