Okay. Welcome back. This is my solution
to the section 18 exception handling challenge.
I'm in the section 18 workspace in the challenge solution project.
And again, this is my solution. There's different ways to do this.
If you did it a different way, that doesn't mean it's wrong, it's just different.
Post it, and look at some comments from everyone. So in this case,
let's take a look at what what I did.
I've created the illegal balance exception that we had earlier,
but I changed it a little bit because what we want now is we want the class illegal balance
exception to be derived publicly from std exception.
Okay. So that takes care of that part.
So now remember, an illegal balance exception now is a std exception.
I've got my public modifier here. Here's my constructor,
I'm just using a default constructor and I'm
guaranteeing no exceptions being thrown from it.
I've got a destructor here again a default destructor.
And here's my virtual what function.
And here's the prototype forward, it's virtual, it returns a pointer
to a character, which is a c-style string.
And it's const -- the function itself is constant won't modify anything.
And again, I'm guaranteeing no exceptions being thrown from here.
And all I'm returning here is any kind of c-style string,
in this case, illegal balance exception.
And this is what we're going to print out on the other side when we catch this exception
and print out the message. So that takes care of illegal balance exception.
Now let's take a look at insufficient funds exception which is right here.
And neither one of these needs a cpp file because we're doing everything right in here.
So here's the insufficient funds exception, pretty much the same pattern.
My class is insufficient funds exception. It's derived publicly
from std exception.
And I've got insufficient funds exception.
Again, no except, default constructor, default destructor.
And there's my what virtual function,
which simply returns insufficient funds exception.
That's it. Now if I go to my account cpp file,
you can see here in my account constructor,
if my balance -- if I'm trying to create
an account object with a balance that's negative, I'm throwing a legal balance exception.
And down here my withdrawal method,
if the balance goes below 0, I'm throwing an insufficient funds exception.
Okay. That's it. Pretty straightforward.
Now let's take a look at the main where we can test some of this.
So here's the main, and
what I'm doing here is I'm creating two unique pointers, one to Moe's account
one to Larry's account.
And then the first thing I want to do is I want to try to create Larry's account with a negative balance.
Right now I expect this to throw an exception.
And I want to catch it right here, I want to catch the illegal balance exception
and just simply print out x.what,
right. That's going to call my virtual function.
And hopefully, it'll say that I've got a negative balance or a new legal balance.
So that's one test case here. In this test case,
I'm going to try to create Moe's account
as they're both savings accounts by the way. I'm creating Moe's account as a savings account
with a 1000 dollar balance.
And then I'm displaying it. I expect that to work.
Then I'm withdrawing five 500 dollars and displaying it again. I expect that to work.
Now I'm withdrawing a 1000 dollars.
I don't expect that to work. I expect that to throw an exception
because that would create an account now with negative 500, I'd be overdrawn.
So i don't expect this to display.
Okay. And this Larry's account here,
we really don't need -- sorry about that, that was a carryover from before.
Okay. So that's better. So in this case, we're going to catch both these exceptions,
an illegal balance exception and an insufficient funds exception.
If either one of them throws, then I catch them and just display their what.
Okay. So let's run this.
And here's my output.
First one is I've got an illegal balance exception. That's what I expected because I tried to
create Larry's account here with negative balance, right.
Then down here, remember, at this point,
I don't display Larry's account because Larry's account hasn't been created.
So what I do is I just skip down to here, and now I'm in this piece of code here.
So the first thing I want to do is i want to try to create Moe's account with a 1000 dollars and display it.
And you can see right here, Moe's account with a 1000 dollars.
There's no interest rate because I didn't supply one.
Then I'm going to withdraw 500 from Moe's account and display it again.
So now you can see Moe's account now has 500 balance right here.
Now I'm going to try to withdraw a 1000 dollars that should not work.
So I shouldn't see Moe's account displayed again. Instead,
I should get an insufficient funds exception being thrown,
and that's exactly what happens here.
This catch block fires a second catch block
and ex.what is displaying insufficient funds exception.
That's it. Most likely your code is very much very similar to mine.
Your test cases may be different, but that's okay.
You can test this by using nested try statements as well.
You can have one try inside another, you can try to create the object.
And then once you've created the object, you can create
try statements within that try statements that are exercising some of the withdrawal methods.
So that's it. I hope you had fun in this section and in this challenge.