WEBVTT

0
00:00.630 --> 00:04.580
In this lesson, I'll show you how you can generate your own exceptions 

1
00:04.580 --> 00:07.470
in case your code encounters unexpected situations.

2
00:08.250 --> 00:13.140
So these are the four keywords are most important when it comes to error

3
00:13.140 --> 00:15.360
handling and catching exceptions.

4
00:15.870 --> 00:20.790
The final keyword that is related is one called raise.

5
00:21.450 --> 00:25.890
And what this allows us to do is to raise our own exceptions.

6
00:27.090 --> 00:27.930
For example,

7
00:27.960 --> 00:32.730
let's say I decide that at the very end of all of our file,

8
00:32.970 --> 00:37.140
no matter if this file went through or if it didn't, if there were an error

9
00:37.140 --> 00:38.160
if there wasn't error,

10
00:38.520 --> 00:43.110
I'm still going to raise an exception. And to raise an exception

11
00:43.140 --> 00:47.460
all you have to do is tap into one of the known exception classes.

12
00:47.670 --> 00:52.020
So let's say I just decided to raise a key error. Now, when I hit run

13
00:52.050 --> 00:56.250
you can see no matter what happened, and in fact, there is no error.

14
00:56.490 --> 01:00.270
I decided to raise this error and that's what I see right here.

15
01:00.570 --> 01:02.760
I could change that to a type error,

16
01:03.150 --> 01:05.640
and that is what will crash my code.

17
01:06.150 --> 01:10.230
And I can even specify a message along with this error.

18
01:10.230 --> 01:14.670
So I could say raise a type error and let's just say the message is,

19
01:17.250 --> 01:21.510
and you can see now when it runs, it hits that type error. And it says,

20
01:21.570 --> 01:26.490
this is an error that I made up. So when might you want to raise errors?

21
01:27.210 --> 01:27.630
Well,

22
01:27.630 --> 01:31.620
let's comment out all the code that we have because we have that type error at

23
01:31.620 --> 01:34.320
the end so it's always going to crash the code no matter what.

24
01:34.590 --> 01:37.200
So let's comment it out and let's start from scratch.

25
01:37.560 --> 01:41.550
Let's say that I decided to calculate the body mass index or the BMI of

26
01:41.550 --> 01:46.080
somebody's and I'm getting them to input their height as a float.

27
01:46.140 --> 01:50.340
So this is going to be their height in meters and their weight as an integer.

28
01:50.880 --> 01:55.880
Now I can calculate the BMI by taking the weight / height * height. 

29
02:00.360 --> 02:05.360
And you can either do this and wrap it inside some parentheses,

30
02:05.700 --> 02:09.240
or you can use the power function or even better

31
02:09.240 --> 02:13.950
you can actually use the builtin exponent, so height to the power of 2.

32
02:15.270 --> 02:18.870
No matter which way we use, we can print out the BMI.

33
02:19.140 --> 02:24.140
But if I provide a height that is just an unrealistic non-human height,

34
02:24.270 --> 02:27.180
lets say there were 45 meters tall,

35
02:27.270 --> 02:29.880
like some sort of four storey building.

36
02:30.390 --> 02:35.070
And then I gave them a weight, so let's say they are 67 kilos.

37
02:35.400 --> 02:39.360
Then obviously a mistake has been made somewhere in the height, right?

38
02:39.360 --> 02:42.210
Because this is just not within the normal human range.

39
02:42.210 --> 02:45.990
This is like Godzilla's height. If I go ahead and hit enter,

40
02:46.260 --> 02:50.190
you can see that we get at BMI and it is calculated correctly

41
02:50.520 --> 02:54.870
and there are no errors because everything is perfectly valid other than the

42
02:54.870 --> 02:59.040
fact that this height should not really ever go over 3 meters.

43
02:59.740 --> 03:03.100
In this case, we might want to raise our own exception.

44
03:03.520 --> 03:08.520
So we can go ahead and say that if the height is greater than 3 meters,

45
03:11.050 --> 03:13.720
well, in that case, it's probably not a valid height.

46
03:14.020 --> 03:17.560
So we can go ahead and raise a value error.

47
03:17.950 --> 03:22.210
So this is an error that says that whatever value was entered

48
03:22.570 --> 03:25.210
as the argument is probably wrong.

49
03:25.720 --> 03:28.300
And we can accompany that with a message.

50
03:29.410 --> 03:33.310
Human height should not be over 3 meters.

51
03:34.780 --> 03:38.920
Now, when we run our code and the user mistakenly types

52
03:38.980 --> 03:43.540
a wrong height, then it's going to actually give us this error

53
03:43.570 --> 03:47.290
and it won't proceed to give them an inaccurate BMI.

54
03:47.980 --> 03:51.310
So this is how you might raise your own exceptions

55
03:51.640 --> 03:55.360
when there are certain things that are not caught by the code because it's

56
03:55.360 --> 04:00.340
perfectly valid code, but it's in fact going to generate the wrong results.

57
04:01.960 --> 04:05.950
Now that we've seen all of these aspects of exceptions,

58
04:06.400 --> 04:10.300
I want you to have a go at catching some exceptions that can commonly occur.

59
04:10.810 --> 04:13.840
So head over to the next lesson where I've got an exercise for you.