1

00:00:00,630  -->  00:00:06,470
Welcome to lesson 11 which roughly covers pages 72 and 73 of automate the boring stuff with Haiphong

2

00:00:06,480  -->  00:00:12,870
textbook right now getting an error or exception in your play on program means that the entire program

3

00:00:12,870  -->  00:00:14,000
will crash.

4

00:00:14,040  -->  00:00:15,900
You don't want this to happen in real world programs.

5

00:00:15,900  -->  00:00:21,780
Instead you want the program to be able to detect errors handle them and then continue to run.

6

00:00:21,810  -->  00:00:23,970
We can make error happen easily.

7

00:00:23,970  -->  00:00:26,430
So in a new file entry the following

8

00:00:29,000  -->  00:00:42,180
def div 42 by Divide by this will be a function that just returns the value 42 divided by whatever argument

9

00:00:42,210  -->  00:00:51,180
we passed in we can call this here print to 42 by dividing by two

10

00:00:55,270  -->  00:00:58,830
divided by 12.

11

00:00:58,830  -->  00:01:03,090
Divide by zero and divided by 1

12

00:01:06,280  -->  00:01:13,380
and I'll just save this as an example that Y will run this program.

13

00:01:13,500  -->  00:01:14,150
So we'll see.

14

00:01:14,160  -->  00:01:16,180
This is caused an error.

15

00:01:17,070  -->  00:01:22,270
This line worked just fine it called div div 42 by and it passed to.

16

00:01:22,380  -->  00:01:30,150
So it returned 42 divided by two which was just 21 that backup printed to the screen and we also called

17

00:01:30,150  -->  00:01:35,160
it by passing 12 so 42 divided by 12 returned 3.5.

18

00:01:35,460  -->  00:01:40,210
But an error happened here when we passed in 0 42 divided by 0.

19

00:01:40,290  -->  00:01:48,130
Cause this 0 division error computers don't really know how to handle dividing a number by zero.

20

00:01:49,050  -->  00:01:54,600
So whenever that happens it says well I don't want to continue running the instructions in this program

21

00:01:54,600  -->  00:01:58,780
because I've just come across this area that I don't know how to handle.

22

00:01:58,890  -->  00:02:01,520
So instead it crashes and terminates the program.

23

00:02:01,560  -->  00:02:06,180
That's why this function call never gets made because the program crashed.

24

00:02:06,180  -->  00:02:07,360
At this point.

25

00:02:07,740  -->  00:02:10,110
But you can handle an error with a try.

26

00:02:10,110  -->  00:02:11,640
Except statement.

27

00:02:11,770  -->  00:02:12,330
The program.

28

00:02:12,330  -->  00:02:14,970
So it looks like this.

29

00:02:15,870  -->  00:02:17,070
Try colon

30

00:02:20,700  -->  00:02:33,680
return 42 divided by Divide by and accept zero division error that's this errors name right here.

31

00:02:34,710  -->  00:02:44,690
Colon print where you try to divide by zero.

32

00:02:44,700  -->  00:02:51,540
Now when we run this program in C it divides by two it divides by 12.

33

00:02:51,570  -->  00:02:55,360
It tries to divide by zero which causes this error to happen.

34

00:02:55,530  -->  00:02:57,330
And it runs this code.

35

00:02:57,330  -->  00:03:02,670
And since there is no return statement this function just ends up returning the none value which gets

36

00:03:03,420  -->  00:03:07,270
printed here and then the program continues on it.

37

00:03:07,280  -->  00:03:09,990
Calls div 42 by and passes 1 to it.

38

00:03:09,990  -->  00:03:12,870
So 42 divided by 1 is 42.

39

00:03:13,110  -->  00:03:15,180
And the program just ran to completion.

40

00:03:15,180  -->  00:03:19,760
It didn't have any sort of ugly error message that appeared.

41

00:03:20,240  -->  00:03:28,440
This happens because when code in a try clause causes a 0 division error the program execution immediately

42

00:03:28,440  -->  00:03:35,250
moves to the code inside the except clause and after running that code except the execution continues

43

00:03:35,250  -->  00:03:36,020
running as normal.

44

00:03:36,030  -->  00:03:39,570
This keeps the program from crashing entirely.

45

00:03:39,570  -->  00:03:45,210
You can also have a simple accept statement without specifying the type of error it catches and it will

46

00:03:45,210  -->  00:03:47,490
catch all types of errors.

47

00:03:47,490  -->  00:03:49,770
This code can be useful for input validation.

48

00:03:49,770  -->  00:03:52,550
Say I have a program where I enter the number of cats I own.

49

00:03:52,590  -->  00:03:53,670
Might look like this

50

00:04:08,080  -->  00:04:24,330
.

51

00:04:27,000  -->  00:04:29,910
So here it asks how many cats do you have.

52

00:04:29,910  -->  00:04:35,370
The user can type in how many they have it's assigned to numb cats and since input returns a string

53

00:04:35,370  -->  00:04:40,470
value we have to convert that to an integer and then that's compared to see if it's greater than or

54

00:04:40,500  -->  00:04:41,440
equal to 4.

55

00:04:41,490  -->  00:04:43,050
And if so print out.

56

00:04:43,050  -->  00:04:44,190
That's a lot of caps.

57

00:04:44,190  -->  00:04:45,360
Otherwise it prints out.

58

00:04:45,360  -->  00:04:49,200
That's not that many cats so let's run this program.

59

00:04:49,200  -->  00:04:50,740
How many cats do you have.

60

00:04:50,870  -->  00:04:53,580
I'll just enter 5 says that is lot like cats.

61

00:04:53,580  -->  00:04:55,340
Let's run that again.

62

00:04:55,350  -->  00:04:56,880
How many cats do you have.

63

00:04:56,880  -->  00:04:57,680
Zero.

64

00:04:57,960  -->  00:05:00,040
It's not that many cats.

65

00:05:00,210  -->  00:05:02,240
There's a possible problem with this code though.

66

00:05:02,310  -->  00:05:06,380
User can type in anything they don't have to type in a number.

67

00:05:06,570  -->  00:05:13,620
So when I run this program I could type in 6 and then that causes an error.

68

00:05:14,070  -->  00:05:20,970
This error happens because the string 6 assigned to the variable name cats is then passed to the function

69

00:05:21,060  -->  00:05:25,250
which is expecting a string that has numeric digits in it.

70

00:05:25,380  -->  00:05:28,630
It works just fine if you have a string like 6.

71

00:05:28,680  -->  00:05:35,250
Soon as you try to pass it a string that doesn't have numeric digits in it like the word 6 then it causes

72

00:05:35,400  -->  00:05:37,260
a value error.

73

00:05:37,560  -->  00:05:40,250
So we can add a try except block in sight here

74

00:05:40,250  -->  00:05:46,640
.

75

00:05:49,710  -->  00:05:53,270
To handle this error except value for

76

00:05:56,190  -->  00:05:57,910
print.

77

00:05:58,070  -->  00:05:59,640
You did not enter page number

78

00:05:59,640  -->  00:06:07,480
.

79

00:06:07,740  -->  00:06:14,610
Now when I run this program I can type anything I want and it will give me an error message saying you

80

00:06:14,610  -->  00:06:19,890
did not enter a number but that's a lot better looking than just this ugly looking Python error message

81

00:06:19,900  -->  00:06:20,540
.

82

00:06:20,960  -->  00:06:23,740
And this is what we mean by input validation.

83

00:06:23,790  -->  00:06:28,980
It validates the input that the user has given us so that it doesn't cause the program to crash just

84

00:06:28,980  -->  00:06:29,760
for extra credit.

85

00:06:29,760  -->  00:06:35,370
You might want to change this program because technically a user could enter something like negative

86

00:06:35,370  -->  00:06:42,750
for cats which is technically not greater than or equal to 4 so it prints out that it's not that many

87

00:06:42,750  -->  00:06:43,780
cats.

88

00:06:43,980  -->  00:06:49,170
See if you can figure out some code to add to this program that will change it so that it detects when

89

00:06:49,170  -->  00:06:53,910
the user enters a negative number and displays a different error message.

90

00:06:53,940  -->  00:06:54,930
To recap.

91

00:06:54,950  -->  00:06:56,210
Divide by zero error.

92

00:06:56,220  -->  00:07:00,750
Happens when ever the python program tries to divide a number by zero.

93

00:07:01,290  -->  00:07:06,720
Errors usually cause the program to crash entirely because Python doesn't know how to continue on after

94

00:07:06,720  -->  00:07:07,090
that.

95

00:07:07,170  -->  00:07:11,020
It doesn't want to run the instructions with bad data.

96

00:07:11,100  -->  00:07:16,170
So we just Holts the entire program and displays this error message but an error that happens inside

97

00:07:16,170  -->  00:07:22,110
a try block will cause code in the accept block to execute that code can handle the error or display

98

00:07:22,110  -->  00:07:26,880
a message to the user or whatever you want it to do so that the program can keep going.
