WEBVTT

00:00.120 --> 00:00.660
Hello again!

00:00.990 --> 00:04.500
In this video, we are going to look at the throw exception specifier.

00:05.910 --> 00:08.310
We have mentioned the "no-throw" guarantee.

00:08.700 --> 00:12.210
This means that an operation does not throw any exceptions.

00:13.200 --> 00:19.080
When we are writing code and we are trying to make it exception safe, whether we are providing the basic

00:19.230 --> 00:21.900
exception guarantee or the strong guarantee,

00:22.500 --> 00:24.520
it is very useful to know that

00:24.540 --> 00:30.510
a function we are calling, or an operation that we are using does not throw exceptions. That makes our life easier.

00:31.440 --> 00:37.380
And we have some help from C++, because none of the functions and operators in the core language or the

00:37.380 --> 00:46.260
library throw exceptions, apart from new, which can throw bad_alloc, dynamic_cast which can throw bad_cast, and

00:46.350 --> 00:48.270
obviously throw, which can throw anything!

00:50.690 --> 00:51.620
In C++,

00:51.620 --> 00:54.980
there are two ways to say that a function does not throw exceptions.

00:55.580 --> 00:57.680
In this video, we are going to look at the older one.

00:59.060 --> 01:00.710
In C++ 98,

01:01.100 --> 01:07.340
You can take a function definition and put the "throw" keyword after the argument list, and then a pair

01:07.730 --> 01:12.410
of brackets, and then you list the exceptions that the function will throw, or could throw.

01:13.400 --> 01:20.540
So in this case, we have a function which could throw a network connection error exception or a data error.

01:23.220 --> 01:28.860
If, for some reason, the function throws some other exception, one which is not in this list, then the

01:28.860 --> 01:31.260
program will be terminated immediately, by default.

01:34.470 --> 01:39.720
We can also have an empty argument list, which means that the function does not throw any exceptions

01:39.720 --> 01:42.420
at all, or at least it is not supposed to!

01:43.440 --> 01:46.440
Again, if it does throw an exception, then the program gets terminated.

01:48.150 --> 01:52.170
The problem with this is that the list of exceptions is not checked.

01:52.830 --> 01:58.920
You can put anything in there, so long as it is a valid C++ type, and the compiler will accept it.

01:59.840 --> 02:01.140
Then your program will run.

02:02.520 --> 02:08.190
If the list of exceptions is not correct, then there is a risk that your program could be terminated

02:08.190 --> 02:08.940
unexpectedly.

02:09.450 --> 02:13.110
And this actually happened to me many years ago, when I started a new job.

02:13.680 --> 02:19.620
I was given some source code, and told to look at it. So I compiled it and ran it, to see what happens.

02:20.220 --> 02:22.410
And it immediately crashed, without any explanation.

02:23.040 --> 02:27.330
It turns out there was some configuration I needed to do, which was not documented.

02:28.230 --> 02:33.120
But the reason why it crashed is that the configuration error threw an exception, and that was in a

02:33.120 --> 02:36.570
function which had an exception list, and the exception was not in the list.

02:37.140 --> 02:39.420
So the program was terminated.

02:40.740 --> 02:44.970
So really, when you call a function which has an exception list, you are trusting that the person

02:44.970 --> 02:47.910
who wrote that exception list did the job properly.

02:49.410 --> 02:52.770
And this is a bit like writing documentation or updating comments.

02:53.430 --> 02:55.890
We all know we are supposed to do it, but it is never the right time.

02:58.420 --> 03:03.910
This throw() exception specifier was actually not very popular, and it has now been removed from the language.

03:04.540 --> 03:09.220
In C++11, it was deprecated, which means you are not supposed to use it anymore.

03:10.390 --> 03:14.770
In C++17, you are no longer able to use throw() with an argument list.

03:15.580 --> 03:19.180
And finally, in C++ 20, it was removed altogether.

03:19.510 --> 03:20.980
So it is now extinct.

03:22.760 --> 03:26.840
We now have something called "noexcept", which I am going to look at in the next video.

03:27.740 --> 03:29.840
So that is all for now, but I will see you next time.

03:30.440 --> 03:32.310
Until then, keep coding!
