- [Instructor] The standard exception class
is specifically designed to declare objects
to be thrown as exceptions.
The exception class has a virtual function called what,
that returns a C string explaining the error condition.
This function can be overloaded in derived classes
to provide a description of the exception.
This is exception.cpp from chapter nine
of the exercise files.
We'll get back to this class in a moment.
First, we're going to start with this broken function,
which is basically just a function
that throws a generic standard exception.
And down here in main,
we say let's have an emergency
and we call the broken function.
Now, when I run this, it's going to go a little bit crazy.
It builds, it runs, it brings up the debugger.
It leaves it running
because it wants me to be able to debug the problem.
We can see we have this signal, SIGABRT.
It's just a bit of a mess.
So I'm going to go ahead, I'm going to close debugger,
I'm going to stop the running code
by pressing Command + .
And that's how you do that here in Xcode.
This will all look different in Microsoft Windows,
but it's actually a little bit easier to deal with.
And you notice we have this cryptic message down here
about an uncaught exception.
So what's happened here,
well, what's happened here is we've called an exception
and we didn't do anything to catch it.
We can catch the exception with a try block,
which looks like this.
And I can take this call to broken,
put it inside the try block,
and then say catch,
which catches the exception.
And I say standard exception
and a reference to a new variable called e,
and then I use that e object,
which is an exception object now.
And I say standard cout,
e.what and standard endl.
And now what's going to happen is,
when we call the broken function,
we catch the exception
and we display its message.
And we can continue operating
or we can exit here or do whatever we want.
And so I'm going to go ahead and build and run this again.
And you see now,
this is all just a little bit more friendly, isn't it?
I called the broken function,
it says, "This is a broken function."
And there's my exception string,
which is not terribly useful, but I've caught it,
and I didn't blow up and start the debugger
and do all of that other stuff.
So how can we make this message more useful?
Well, we can make this message more useful
by inheriting our own exception class
and inheriting from the standard exception class.
And that's what we do here.
You notice I put this in my own namespace.
I tend to do this with my own classes a lot.
And my namespace is usually named BW,
you can name yours whatever you like.
And I declare a class,
and this class inherits from exception.
We haven't talked about inheriting.
We'll talk about that more in the advanced topics,
companion course, but here we're inheriting
from the exception class,
we have our own little place for a message.
The standard default constructor
is putting private so that we can't call it.
And we have a public constructor that takes one argument,
which is a character string.
And it has this noexcept (true),
which is a specifier that basically says that,
no, this is not going to throw an exception.
So it's safe to use it in this context.
And we have that also on our what function.
And our what function just basically returns a message.
And so that's all it does.
And so now instead of throw standard exception,
I can throw BW:: E and give it a message.
And now when I catch, I want to catch my BW::E,
and now when I run my code,
it says, ouch.
So that can obviously be whatever message I want.
And this is very convenient.
This is a convenient way to do this,
it's actually pretty common,
but it's not terribly scalable.
If you have a large system,
you can end up with a mess of different error messages.
So it might be useful to define them all in one place.
I can say, const BW::E,
and call this one e_ouch, like that.
And I can do this for a few different ones.
I can have e_bad,
or I can have e_worse.
And now, instead of throwing this,
I can say, throw e_ouch,
or I can say any one of 'em.
And when I build and run this,
now I get don't do that.
So this is a handy way
to create a bunch of standard error messages
for a large piece of code,
or just to declare a class
and specify it wherever you use it.
So C++ exceptions provide a simple and effective way
of handling errors
and other exceptional circumstances at runtime.
The standard exception class is well-suited
for deriving objects
to be thrown as exceptions for a variety of usages.