Hello. Welcome to the section 18 challenge on exception handling.
Congratulations on finishing this section.
This challenge is a very straightforward challenge,
but it's going to be using some interesting techniques.
One of the things that I want you to do in this challenge is to
derive your own exception classes from
the standard libraries exception class
and then integrate that into the account class and then test your code.
Now I've got you started here,
I've created a challenge project and again I'm in the section 18 workspace in the challenge project.
And under here, I've got all the source code for the account class
that we've been working with throughout the course.
This is the description.txt file, which is what we're looking at right now.
And I'd like to read through this in a minute, and then I'll talk about the different exception classes.
Okay. So for this challenge what we want you to do is to integrate
your own user-defined exception classes into the account class.
The account classes are provided for you. You can see them over here.
Also the illegal balance exception class
already has been created for you, but it does not derive from
the std exception class. You have to do that yourself.
What I'd like you to do is derive the illegal balance exception class,
like I just said from std exception and implement the what method.
Okay. That what method is that virtual method that returns a character string, a c-style string
and provide an exception message there.
Now this exception object should be thrown from the account class constructor
whenever we get a negative balance.
Okay. So in other words, if you try to create an account with a negative balance,
we want to throw this, very much like what I did in the slides.
So that's one class. The second class is a class called
insufficient funds exception, you can see that right here.
Again, I want you to derive this from std exception
and implement the what method for it to provide a message.
This exception object should be thrown
if a withdrawal results in a negative balance, so you're overdrawn.
Okay. You should throw this exception object from the accounts withdraw method.
That's it. Pretty straightforward. The harder part of this
assignment is not so much implementing these classes, it's testing them,
and you'll have to do that in your main.
All right. So let me go over this illegal balance exception class right here.
There's no cpp file, it's all in the header file.
And I've created a class called illegal balance exception.
It's got a default constructor and a default destructor.
And I've got those curlies here, you could replace those with equal default if you like.
I think I did that in the solution, but you don't have to.
So that's it. Now what I want you to do is
public inheritance here, std exception and then implement the what method.
You'll notice that that's already there.
And if you go to account cpp
in the account constructor here on line three,
I'm checking to see if the balance is less than 0.
If it is, I'm throwing a legal balance exception.
Okay. So that's part one.
The second part is I want you to create
let me go back to my description real quick.
I want you to create another class called insufficient funds exception,
same idea as illegal balance exception.
It's going to be derived publicly from std exception.
You're going to have, your constructor your destructor, and you're going to implement that
what virtual function again.
And then that exception will be thrown
here in the account cpp withdraw method right here.
So you can see that if the balance goes negative,
you want to throw that exception object.
Okay. Now where do you catch these things. Well,
let's go to our main.
This is where you want to test your code. This is where you want to create
your checking account objects, your savings account objects and so forth,
create them with negative balances,
put a 100 dollars in it, and then withdraw 200 dollars from it so you get an um
insufficient funds exception. So that's the kind of thing you want to do here.
You want to be sure you do that in a try block and.
You want to be sure that you're catching those exception objects here.
Okay. So that's really it. It's not a lot of code to write.
It's just writing them in precise locations and getting it to work just right.
All right. So good luck.
I'll see you on the next video where I'll show you my solution.