Hello again!
In this video, we are going to look at the throw exception specifier.
We have mentioned the "no-throw" guarantee.
This means that an operation does not throw any exceptions.
When we are writing code and we are trying to make it exception safe, whether we are providing the basic
exception guarantee or the strong guarantee,
it is very useful to know that
a function we are calling, or an operation that we are using does not throw exceptions. That makes our life easier.
And we have some help from C++, because none of the functions and operators in the core language or the
library throw exceptions, apart from new, which can throw bad_alloc, dynamic_cast which can throw bad_cast, and
obviously throw, which can throw anything!
In C++,
there are two ways to say that a function does not throw exceptions.
In this video, we are going to look at the older one.
In C++ 98,
You can take a function definition and put the "throw" keyword after the argument list, and then a pair
of brackets, and then you list the exceptions that the function will throw, or could throw.
So in this case, we have a function which could throw a network connection error exception or a data error.
If, for some reason, the function throws some other exception, one which is not in this list, then the
program will be terminated immediately, by default.
We can also have an empty argument list, which means that the function does not throw any exceptions
at all, or at least it is not supposed to!
Again, if it does throw an exception, then the program gets terminated.
The problem with this is that the list of exceptions is not checked.
You can put anything in there, so long as it is a valid C++ type, and the compiler will accept it.
Then your program will run.
If the list of exceptions is not correct, then there is a risk that your program could be terminated
unexpectedly.
And this actually happened to me many years ago, when I started a new job.
I was given some source code, and told to look at it. So I compiled it and ran it, to see what happens.
And it immediately crashed, without any explanation.
It turns out there was some configuration I needed to do, which was not documented.
But the reason why it crashed is that the configuration error threw an exception, and that was in a
function which had an exception list, and the exception was not in the list.
So the program was terminated.
So really, when you call a function which has an exception list, you are trusting that the person
who wrote that exception list did the job properly.
And this is a bit like writing documentation or updating comments.
We all know we are supposed to do it, but it is never the right time.
This throw() exception specifier was actually not very popular, and it has now been removed from the language.
In C++11, it was deprecated, which means you are not supposed to use it anymore.
In C++17, you are no longer able to use throw() with an argument list.
And finally, in C++ 20, it was removed altogether.
So it is now extinct.
We now have something called "noexcept", which I am going to look at in the next video.
So that is all for now, but I will see you next time.
Until then, keep coding!