WEBVTT

00:00.150 --> 00:03.120
Hello again! In this video, we are going to look at error handling.

00:04.590 --> 00:07.080
We are not talking here about bugs in your code.

00:07.440 --> 00:12.150
These are things that can go wrong at run time, when your program interacts with the real world.

00:12.750 --> 00:17.550
So, for example, if you try to read data from a file and the file does not exist. If you are

00:17.550 --> 00:23.580
trying to send data over a network and the connection is down. If you receive data which is incorrect,

00:23.610 --> 00:25.200
or in the wrong format. And so on.

00:25.740 --> 00:31.770
So these are situations which arise at run time, when the environment does not behave in the way that

00:31.770 --> 00:33.150
your program is expecting.

00:35.970 --> 00:41.700
If we are just writing toy programs, we can print out a message and say goodbye, but if we want to

00:41.700 --> 00:45.900
watch professional quality programs, we need to do a lot better than that.

00:50.170 --> 00:53.260
When an error occurs, we need to communicate with the user.

00:53.650 --> 00:57.250
We need to say what the problem is, and what actions the user can take.

00:57.880 --> 00:59.500
Normally there are three possibilities.

01:00.070 --> 01:00.880
They can try again.

01:01.540 --> 01:05.590
They can do something to try and resolve the problem, or they can just ignore it and carry on.

01:06.920 --> 01:10.670
We may also need to inform other concerned parties.

01:11.150 --> 01:16.370
For example, if our company's connection to the internet is down, we may need to notify the network

01:16.370 --> 01:17.120
management team.

01:18.980 --> 01:21.810
We should avoid giving information which is not helpful to the user.

01:21.830 --> 01:27.770
So things like error numbers, or Java stack traces. which may be helpful to the developer, but they are

01:27.770 --> 01:31.010
completely unhelpful to the person stuck in front of the computer.

01:31.460 --> 01:35.660
If the situation really is beyond help, then we can exit the program.

01:35.870 --> 01:37.490
But that really should be your last resort.

01:40.310 --> 01:41.630
Where should we handle errors?

01:43.100 --> 01:49.220
The obvious answer is to handle the error where it occurs. And quite often that is going to be good enough.

01:49.790 --> 01:54.800
For example, if you are asking for user input and they type in something which is in the wrong format,

01:55.160 --> 01:56.270
we can ask them to try again.

01:57.290 --> 01:59.930
But there are situations where it can be useful to handle

01:59.930 --> 02:02.840
errors in a different part of the code where it occurs.

02:05.260 --> 02:09.700
As an example, if we are downloading a file, so we have some networking code, at a low level,

02:09.700 --> 02:11.860
which is doing the actual fetching of the data.

02:12.340 --> 02:17.230
And suppose that when something goes wrong with the networking code, we want to display a dialogue box.

02:18.730 --> 02:23.800
If we decide to handle the error where it occurs, that means that every time there could be an error,

02:24.130 --> 02:27.160
we need to write some code to display the dialogue box.

02:27.700 --> 02:32.200
So this means we are going to have a lot of GUI code mixed in with our networking code, which is going

02:32.200 --> 02:33.940
to make the code more complicated.

02:34.720 --> 02:38.890
If we are trying to follow the logic of the networking, that is going to be difficult, because there is

02:38.890 --> 02:42.970
all this user interface code that keeps jumping up and distracting us.

02:44.980 --> 02:49.920
And if we adopt this approach for every feature that our program has, then we are going to have error handling

02:50.260 --> 02:51.440
spread all over the program.

02:51.790 --> 02:57.610
There is probably going to be a lot of duplicated code. And also, it is going to be hard to work out where

02:57.610 --> 03:00.580
the code that produces a particular dialog box comes from.

03:01.090 --> 03:03.910
So this is going to be code which is going to be hard to maintain.

03:07.460 --> 03:13.430
So in this situation, a better approach would be to have a specialized part of the code, which just deals with

03:13.430 --> 03:14.090
error handling.

03:14.510 --> 03:19.400
And then when an error occurs, we can send it off to this part of the code. And that will display the

03:19.400 --> 03:21.230
dialogue box, or whatever we need to do.

03:21.920 --> 03:26.990
So that means we need to be able to transfer the information about the error, from the place where the

03:27.230 --> 03:29.450
occurs, to the code which handles it.

03:30.050 --> 03:33.260
And we are going to be talking about how to do that in the rest of this section.

03:34.250 --> 03:35.750
Okay, so that is it for this video.

03:36.170 --> 03:37.040
I will see you next time.

03:37.040 --> 03:39.370
But until then, keep coding!
