WEBVTT

00:00.240 --> 00:06.060
Hello again! In this video, we are going to look at exception safety. Exception safety means that

00:06.060 --> 00:11.280
code behaves correctly when exceptions are thrown, and obviously that is something we want.

00:11.880 --> 00:15.900
So how do we do it? In C++,

00:15.910 --> 00:18.650
there are three main ways to write exception-safe code.

00:19.320 --> 00:25.830
You can provide the so-called basic exception guarantee: if an exception is thrown during an operation,

00:26.250 --> 00:27.810
no resources will be leaked.

00:28.320 --> 00:33.180
So, for example, if you open a file and an exception is thrown, the file gets closed.

00:33.720 --> 00:40.050
If you allocate memory in an operation and an exception is thrown, the memory is released. And so on.

00:42.560 --> 00:48.350
The strong exception guarantee provides all that, and it also reverts the program to its previous

00:48.350 --> 00:50.510
state, before the operation started.

00:51.200 --> 00:53.450
So it is all as if the operation had never happened.

00:53.930 --> 00:54.980
It was all just a dream :)

00:56.320 --> 00:57.670
And then we can have the no

00:57.670 --> 01:00.700
throw guarantee, which is that we do not throw any exceptions at all.

01:01.360 --> 01:06.160
And if you do not throw any exceptions, then you do not have any problems from dealing with exceptions.

01:11.430 --> 01:17.160
The basic exception guarantee provides the minimum level of exception safety: an operation

01:17.160 --> 01:19.230
will either succeed, or throw an exception.

01:19.740 --> 01:25.860
And if there is an exception, there are no resource leaks. So all operations and functions in the C++

01:26.160 --> 01:28.650
standard library provide this basic guarantee.

01:30.780 --> 01:36.750
With the strong exception, guarantee, an operation will either succeed or have no effect. If

01:36.750 --> 01:41.610
it throws an exception, everything will go back to the same state as it was before the operation.

01:42.270 --> 01:44.790
And that is what is known as transactional semantics.

01:45.690 --> 01:50.490
If you have worked with databases, you may have come across commit and rollback. When you were doing

01:50.490 --> 01:51.060
an update,

01:51.420 --> 01:55.620
if everything succeeds, then you "commit", and the changes are saved in the database.

01:56.340 --> 02:01.050
If something goes wrong, you "roll back" your changes, and the database is left unmodified.

02:05.300 --> 02:10.820
All or references, which you obtained before the operation, will remain valid after

02:10.820 --> 02:14.000
the operation, even if the operation throws an exception.

02:15.320 --> 02:21.050
And all the operations on the standard templates library containers provides this strong exception guarantee.

02:21.350 --> 02:28.730
There are just a couple of cases within search operations where iterators can be invalidated. To provide

02:28.730 --> 02:30.860
the basic exception guarantee,

02:31.160 --> 02:38.150
any resources which are acquired during the operation must be released when an exception is thrown.

02:38.840 --> 02:40.640
So you can either do that manually.

02:41.240 --> 02:43.850
You have a try block where you acquire resources.

02:44.330 --> 02:50.060
For example, allocating memory. And in the catch block, you release the resource - you call delete.

02:50.990 --> 02:56.510
Or alternatively, you can use objects which manage the resource and automatically release it when the

02:56.510 --> 02:57.320
destructor is called.

02:57.860 --> 03:03.440
So that will be when the program jumps out of the try block and calls the destructors for all the objects

03:03.440 --> 03:03.860
in the try

03:03.860 --> 03:04.100
block.

03:04.940 --> 03:09.710
So with string and vector, that will release the memory that is being used by the object.

03:10.280 --> 03:13.010
And for fstream, the destructor will close the file.

03:13.700 --> 03:15.920
So this should make you think of a four-letter word.

03:16.440 --> 03:17.660
No, not that four-letter word!

03:18.350 --> 03:22.040
I am thinking of RAII, but we will come back to that.

03:22.730 --> 03:24.230
Okay, so that is it for this feature.

03:24.680 --> 03:25.590
I will see you next time.

03:25.590 --> 03:27.950
But until then, keep coding!
