All right, so welcome back to another lecture in this lecture, we're actually just going to go off
on a little side topic.
And this same topic is going to be about exceptions.
It's going to be a short lecture, but I felt like this kind of is a necessary part of programming.
I kind of wanted to go over everything.
Of course, for programming in general, in any language, we're using C++, right?
But these are things that would be present.
And the things that we want to cover in the course are things that you would use in many different programming
languages.
And exceptions are in a lot of languages.
So we're going to definitely go ahead and cover that.
So what are exceptions?
Well, basically think about when you have an error in your program, right?
Like you get some sort of undefined behavior you might try and like put a ton of different like conditional
statements to do certain certain things to handle that.
But, you know, after a while, that can get kind of kind of messy, right?
And you just don't want like a ton of just straight up only conditionals and print statements.
Maybe there's something later down the line that needs to be prevented by you handling errors on your
own right instead of just encountering some error, having the program crash or having like a ton of
spaghetti code trying to deal with like handling errors only with if and else if without anything else.
So exceptions are something that we can kind of add into this game by using F and elseif and stuff like
that, but also adding this other part where we can kind of catch errors and choose what to do when
we catch those errors.
So enough of me kind of just explaining that let me go ahead and show you.
So I'm going to go ahead and just make a little example program here.
I'm actually going to make a function prototype, so I'm going to go over just a couple of quick examples
and they're going to be kind of trivial, but they'll illustrate the point, hopefully.
So first off, I'm going to make something called like, I'm going to say I'm going to do and.
And let's just say double, and let's just say.
Like age divide by two or something like this, so you get an aged into this function and it divides
it by two and tries to, you know, return return something.
Right?
So.
Well, let's just get to let's get his age there by their age or something like that.
So you get like age one and age two, you divide one age by another age.
So let's go ahead and make a main function here.
And so what I'm going to do is just say, into age one.
So let's go ahead and see and let's go ahead and make some major change one.
Let's just say zero and age two equals zero.
And then let's go ahead and say that we can't age one here.
Actually, no, that's why I only do that one.
And that so then we'll say into age to.
Cool.
So and then she managed to.
And then let's go ahead and call this function, so we'll say A..
By other age and I would just say age one age to
finish this stuff right here and then we will go ahead and put this function right here.
So maybe I like returned some like.
Double, let's just say, you know, we'll just call these double so double h one.
Double Page two.
And all of this returns of double, I won't say age.
Dave, it's this age due by their age.
Their age actually should have this be camel case, right?
Age to buy their age have a happy, probably.
So we take this and age, I would say one.
And no, not.
And let's take doubles, right?
So she's the devil.
So let's take like.
Make sure you get this right.
Double one, double A2, right?
Fumbling all over the place.
So we take these and we just try and like return
like A1 divided by a two, right?
We just do that.
So and then we'll go ahead and just say, like, see out this, right?
So let's just say like.
Age one over
and by age to.
Equals that right?
Cool.
So let's go ahead and just save this program, right, and let's go ahead and run this program, so
I'll go down here.
Safety First Bus
called this exceptions exceptions to CBP.
Uh, so eat out.
Actually do that.
OK, so I'm going to run this program now and listen to some ages, so what if I say, let's just do
something simple, right?
Let's just say like six and two.
Yeah, that works fine, right?
But like, let's try something else.
What if I say zero and four?
OK, well, that works.
But let's try four and zero.
Yeah, this is kind of a weird problem, right?
It's just like I and F.
Right?
That's because we can't really divide by zero.
Right.
So this is like some sort of divide by zero error.
So how would we deal with that, right?
Like, we could put some f in here and just say, if you know a two equals zero, then you could just
see out like, hey, you know, zero age or hey, like, can't divide by zero.
You know, like that.
And then this could just be like, you know, we could just return right now or this could be like,
this could be like.
You know, we could just say return, blah blah blah.
You know, we could like return something or like, maybe.
We don't want to like actually do the division right, so we don't want to have this and else, but
then we have to like return something, right?
So.
You know, because if this happens, you need to basically like you definitely want to like return something,
right?
You don't want to just like not return.
So you can just be like, Oh, return, I don't know, return zero and then like.
Then once you return that, then you have to, like, somehow put some some air here and see if it's
zero, then like, I don't know, like don't print it out or something like that and like that word
worked fine.
Right.
But imagine if you like this gets a lot more complex, like it becomes a bigger program and you need
to.
You need to like, you know, handle like a lot of different errors and stuff like this where you have
unexpected behavior and you're just going to end up putting a ton of conditionals.
There's actually a cleaner and more kind of well known way of doing this, like a specific way that
you're supposed to handle these types of situations and that's using these exceptions.
So let's change this into an exception.
So what I'm going to do is I'm going to use a
I'm going to use a keyword it special called Throw.
So we like throw an error message and I'm just going to say, Hey, can't divide by zero, just kind
of like what we did before.
But what I'm going to do is I'm actually going to have a thing up here to be able to catch this error,
and I'm going to try and do this function right here, right?
So I'm going to put a try right here.
So it's the keyword try.
So we used a keyword that was special called throw down here, right up in Maine, where you're using
something called Try.
I put little braces.
I'm going to copy this, and I'm going to put that in here.
So I'm going to attempt this right.
I'm going to try and do this.
Which is like, you know, to print out this and then call this function and tell me the age, divide
it by the other age so then I can catch.
Right?
I can go ahead here and I'm going to do a catch.
So this is catching the thing that gets thrown right.
This throw is basically throwing a message.
The way to do this is you say that you're going to try it right and then the catch, if there is an
error, it's going to catch this and what you have to put in here or this is just kind of a convention
is to put a cost char plan basically to this message shows kind of like a array of characters, you
know, pointing to an array of characters.
I'm going to ask you the same message.
And so when I put this constant message thing in here, it's going to grab these characters right here
and we can actually print it out, so I'll just say, see our message.
So you might be saying like, well, we could just do all these, you know, we can do this if and stuff.
This is actually a cleaner and more professional way of doing this, and you'll notice that sometimes
it can get quite like growth in the code when you're trying to handle like a bunch of different errors
and stuff.
But the cool thing is that you can try a bunch of stuff, you know and get this message thrown back.
You can actually even make your own exception classes, but we're not going to go into that.
Because that's kind of outside the scope of what we're trying to do here, but it's important that you
knew how to at least handle semi some exceptions.
So let's go ahead and try this out.
Let's see.
Let's see what happens.
So we saw what it did before, right, like we got some like infinity or something like, you know,
it just it was just set.
Right?
So let's go ahead and save this and we'll recompile it and see what happens now.
So I'm going to enter age one, let's just say like nine and I introduced to zero and it actually says,
Hey, can't divide by zero, so that's what gets printed out.
So what it did was it said, Oh, hey, two is zero.
I'm going to say, Hey, you can't divide by zero.
Instead of doing this, so this isn't even like an elf, you know, it just immediately throws this
air so we don't have to deal with like any other returns, right?
Or anything like that, we can just have our function be pretty clean and just return this one thing.
If this actually is encountered, it'll just throw this air.
It won't.
It'll say, like, Hey, I tried to do this right?
I tried it specifically tried to call this function.
It didn't work out.
So I caught whatever air was thrown from here, and that was what we were catching right here.
Is this message, right?
And I printed out the message.
So you can do like similar things, you know, with, you know, filtering people's age in a different
way, like you could add another one here.
So let's say that you wanted it to be like, let's say like verify age.
Let's just say, like verify ages and we go down here, and let's just say.
Let's just say try.
Ages are valid.
Let's just say ages are valid.
If.
Let's just make some like let's make this like a Blu ray or something like that.
So we'll verify ages.
And then I'll just say, hi, return true.
And what we will do is like.
We'll just say, like our age is
valid or something, we're actually just saying like.
Pages are.
Ages are valid, right?
Let's just say if.
I know this is kind of like ironic because I said, you don't really have to use that money if I'm not
coming up with, like the best example here, but it's just something to illustrate a point of like
that.
You can do this, you know, in different ways.
You can, like, handle multiple things.
So.
If this return is true, then we'll just say aid is a valid.
And otherwise, you know, we'll just print out some stuff like if the ages are not within our parameters,
so let's say if a two is less than zero.
Or, let's say, actually eight one eight four eight one is less than zero or a two.
It's less than zero.
Then we'll just say like throw like can't have nega tive ages.
And let's just say another thing like nothing else, if a one is greater than like 150 or something,
I mean, that's like pretty, all right.
Let's just say that's like a cap on some age or a two is over 150.
Then we'll just like throw some air and say, I'm
way too old.
Yeah, right?
You know, something like that, I don't know.
I don't need these brackets, actually, since we just have one line.
Cool.
So it's difficult on here, so we could even take these old, I mean, I don't want to be that like,
be that pessimistic, you know, but I don't know anyone that's lived that long, right?
170 That's pretty crazy.
So we can just like now, what we have is something they can verify ages and have a specific area that
gets thrown if they exceed these bounds, you know, so and oh, we know how it's still pretty clean,
right?
It's still pretty clean.
So this function really like could be doing something else, too, like maybe it actually still does.
Age, divide by other age or something like that, let's just say we could even add another one, right?
Like what if we do this?
What if this is like an LCA?
Here will change it back to even what it was before, so if a if a two equals zero, let's just say
like
throw can't divide by zero.
Right.
So.
We're going to have all of these things, and we just change suspected devil, right?
So we'll just return a one by a two.
And now we have like extra checks, actually.
So that's kind of cool, right?
So we'll just say, like, we'll put this back to kind of how it was or just say like see out age one
to five by age two equals and then we'll just stay.
This is a positive by age, right?
So let's just like put that back to how it was.
Um, and yeah, we'll just say age, divide by age, each one age to.
So I know it kind of went back, you know, I went on a little detour around and came back, but, you
know, now we can catch multiple things, so we kind of set it up like, Oh, you know, the aid shouldn't
be allowed to be, you know, zero because we can't abide by that.
The age can.
But at least like, you know, we want to make sure that it's not like negative, right, either because
people shouldn't have negative ages.
Can you say that specifically, right?
Like people can have negative ages and then like this one that's just too old, you know?
So now we can handle all those different ones.
All those different messages can just be caught and displayed right here.
And it's pretty clean looking, right?
Otherwise, we can just do our normal division and just kind of like, you know, carry on with our
code like it would go ahead and just.
You run this and it would skip the catch and keep going if we had like, you know, more co down here.
So let's go ahead and try this out then.
So I'm going to compile this again.
So let's do something like, let's do a normal age, right?
So we'll just say like 60 and 20.
You know, it's just it's like age old divides two is three.
So that that works fine, right?
But let's put something like, let's say, like, uh, like negative four and like Negative 20 or something
or like Negative 20.
So says, Hey, people can't have negative ages, right?
And then we go in here and we say, like, you know, one hundred and ninety and then like ten, you
know, this person in age one 190 is like way too old.
Yeah, right?
So it was able to catch that right and then we can still do our normal on, you know, and it's like,
let's say it's just like 40 and then zero, it says can't fly by zero.
So we're catching all of these problems, right?
Things that wouldn't really work very well if we had some sort of system that was keeping track of like
people's data, right?
We wouldn't want any of these successfully entered into some database or some system and get processed
later on.
We'd rather just like throw some air and then, you know, we could kind of handle things however we
want at that point after this gets thrown like.
But.
The point is, is that it throws this message and doesn't doesn't do anything else.
You know?
So kind of cool, right?
A little bit of a sidetrack, but an important topic, you'll find these exceptions and air handling
things and a lot of other languages like Python and stuff like that.
So really important.
Good to know about it.
And these will be translatable skills.
You know how slightly different syntax, of course, another language, but it'll be cool that you know
about it.
So when you start adding some other languages under your belt, you'll be able to kind of know that
this is a tool that you can use.
All right.
So with that, I will see you in the next lesson.