1
00:00:00,630 --> 00:00:04,580
In this lesson, I'll show you how you can generate your own exceptions 

2
00:00:04,580 --> 00:00:07,470
in case your code encounters unexpected situations.

3
00:00:08,250 --> 00:00:13,140
So these are the four keywords are most important when it comes to error

4
00:00:13,140 --> 00:00:15,360
handling and catching exceptions.

5
00:00:15,870 --> 00:00:20,790
The final keyword that is related is one called raise.

6
00:00:21,450 --> 00:00:25,890
And what this allows us to do is to raise our own exceptions.

7
00:00:27,090 --> 00:00:27,930
For example,

8
00:00:27,960 --> 00:00:32,729
let's say I decide that at the very end of all of our file,

9
00:00:32,970 --> 00:00:37,140
no matter if this file went through or if it didn't, if there were an error

10
00:00:37,140 --> 00:00:38,160
if there wasn't error,

11
00:00:38,520 --> 00:00:43,110
I'm still going to raise an exception. And to raise an exception

12
00:00:43,140 --> 00:00:47,460
all you have to do is tap into one of the known exception classes.

13
00:00:47,670 --> 00:00:52,020
So let's say I just decided to raise a key error. Now, when I hit run

14
00:00:52,050 --> 00:00:56,250
you can see no matter what happened, and in fact, there is no error.

15
00:00:56,490 --> 00:01:00,270
I decided to raise this error and that's what I see right here.

16
00:01:00,570 --> 00:01:02,760
I could change that to a type error,

17
00:01:03,150 --> 00:01:05,640
and that is what will crash my code.

18
00:01:06,150 --> 00:01:10,230
And I can even specify a message along with this error.

19
00:01:10,230 --> 00:01:14,670
So I could say raise a type error and let's just say the message is,

20
00:01:17,250 --> 00:01:21,510
and you can see now when it runs, it hits that type error. And it says,

21
00:01:21,570 --> 00:01:26,490
this is an error that I made up. So when might you want to raise errors?

22
00:01:27,210 --> 00:01:27,630
Well,

23
00:01:27,630 --> 00:01:31,620
let's comment out all the code that we have because we have that type error at

24
00:01:31,620 --> 00:01:34,320
the end so it's always going to crash the code no matter what.

25
00:01:34,590 --> 00:01:37,200
So let's comment it out and let's start from scratch.

26
00:01:37,560 --> 00:01:41,550
Let's say that I decided to calculate the body mass index or the BMI of

27
00:01:41,550 --> 00:01:46,080
somebody's and I'm getting them to input their height as a float.

28
00:01:46,140 --> 00:01:50,340
So this is going to be their height in meters and their weight as an integer.

29
00:01:50,880 --> 00:01:55,880
Now I can calculate the BMI by taking the weight / height * height. 

30
00:02:00,360 --> 00:02:05,360
And you can either do this and wrap it inside some parentheses,

31
00:02:05,700 --> 00:02:09,240
or you can use the power function or even better

32
00:02:09,240 --> 00:02:13,950
you can actually use the builtin exponent, so height to the power of 2.

33
00:02:15,270 --> 00:02:18,870
No matter which way we use, we can print out the BMI.

34
00:02:19,140 --> 00:02:24,140
But if I provide a height that is just an unrealistic non-human height,

35
00:02:24,270 --> 00:02:27,180
lets say there were 45 meters tall,

36
00:02:27,270 --> 00:02:29,880
like some sort of four storey building.

37
00:02:30,390 --> 00:02:35,070
And then I gave them a weight, so let's say they are 67 kilos.

38
00:02:35,400 --> 00:02:39,360
Then obviously a mistake has been made somewhere in the height, right?

39
00:02:39,360 --> 00:02:42,210
Because this is just not within the normal human range.

40
00:02:42,210 --> 00:02:45,990
This is like Godzilla's height. If I go ahead and hit enter,

41
00:02:46,260 --> 00:02:50,190
you can see that we get at BMI and it is calculated correctly

42
00:02:50,520 --> 00:02:54,870
and there are no errors because everything is perfectly valid other than the

43
00:02:54,870 --> 00:02:59,040
fact that this height should not really ever go over 3 meters.

44
00:02:59,740 --> 00:03:03,100
In this case, we might want to raise our own exception.

45
00:03:03,520 --> 00:03:08,520
So we can go ahead and say that if the height is greater than 3 meters,

46
00:03:11,050 --> 00:03:13,720
well, in that case, it's probably not a valid height.

47
00:03:14,020 --> 00:03:17,560
So we can go ahead and raise a value error.

48
00:03:17,950 --> 00:03:22,210
So this is an error that says that whatever value was entered

49
00:03:22,570 --> 00:03:25,210
as the argument is probably wrong.

50
00:03:25,720 --> 00:03:28,300
And we can accompany that with a message.

51
00:03:29,410 --> 00:03:33,310
Human height should not be over 3 meters.

52
00:03:34,780 --> 00:03:38,920
Now, when we run our code and the user mistakenly types

53
00:03:38,980 --> 00:03:43,540
a wrong height, then it's going to actually give us this error

54
00:03:43,570 --> 00:03:47,290
and it won't proceed to give them an inaccurate BMI.

55
00:03:47,980 --> 00:03:51,310
So this is how you might raise your own exceptions

56
00:03:51,640 --> 00:03:55,360
when there are certain things that are not caught by the code because it's

57
00:03:55,360 --> 00:04:00,340
perfectly valid code, but it's in fact going to generate the wrong results.

58
00:04:01,960 --> 00:04:05,950
Now that we've seen all of these aspects of exceptions,

59
00:04:06,400 --> 00:04:10,300
I want you to have a go at catching some exceptions that can commonly occur.

60
00:04:10,810 --> 00:04:13,840
So head over to the next lesson where I've got an exercise for you.

