1
00:00:07,880 --> 00:00:10,980
Welcome back. Now, remember that there are

2
00:00:10,980 --> 00:00:14,430
three main kinds of errors that we can get when writing Python code.

3
00:00:14,430 --> 00:00:17,110
There are syntactic errors.

4
00:00:18,230 --> 00:00:24,135
That's when Python doesn't understand how to read the instructions in your program.

5
00:00:24,135 --> 00:00:30,089
So, for example, if I forget a bracket or a parenthesis and I tried to run my program,

6
00:00:30,089 --> 00:00:32,370
then I get a syntax error saying that

7
00:00:32,370 --> 00:00:35,830
Python didn't understand the instructions that I gave it.

8
00:00:35,870 --> 00:00:41,710
Beyond syntactic error, there are also runtime errors.

9
00:00:44,870 --> 00:00:49,810
Runtime errors occur when Python understood the instructions that I gave it,

10
00:00:49,810 --> 00:00:53,860
but something went wrong when Python tried to run those instructions.

11
00:00:53,860 --> 00:00:56,890
So, for example, if I run my code right now,

12
00:00:56,890 --> 00:00:59,919
I get a runtime error or more specifically

13
00:00:59,919 --> 00:01:03,070
an index error because even though Python understands that I'm

14
00:01:03,070 --> 00:01:06,220
creating a list and assigning that list to items and then

15
00:01:06,220 --> 00:01:10,580
I'm assigning third to be the third item in items,

16
00:01:10,580 --> 00:01:14,565
because item only has two items, a and b,

17
00:01:14,565 --> 00:01:16,460
when I tried to fetch index two,

18
00:01:16,460 --> 00:01:19,525
then I get an index error which is the kind of runtime error.

19
00:01:19,525 --> 00:01:22,590
So, in other words, Python understood what I wanted to do,

20
00:01:22,590 --> 00:01:26,255
but something went wrong as Python was trying to execute my code.

21
00:01:26,255 --> 00:01:29,550
Then finally, there are semantic errors.

22
00:01:31,450 --> 00:01:35,810
In a semantic error is when Python successfully ran the code,

23
00:01:35,810 --> 00:01:38,150
it just wasn't exactly what I wanted it to do.

24
00:01:38,150 --> 00:01:40,610
So, for example, if I wrote a function to add

25
00:01:40,610 --> 00:01:43,700
two numbers and that function actually divided two numbers,

26
00:01:43,700 --> 00:01:46,970
Python isn't going to give me an error but I'm also not going to

27
00:01:46,970 --> 00:01:50,890
be happy when my code divides the numbers that I wanted to add.

28
00:01:50,890 --> 00:01:53,440
So, when we have a syntactic error,

29
00:01:53,440 --> 00:01:55,310
Python doesn't run our code at all.

30
00:01:55,310 --> 00:01:57,250
When we have a runtime error,

31
00:01:57,250 --> 00:02:00,770
Python runs our code until it encounters the runtime error.

32
00:02:00,770 --> 00:02:02,300
Then we have a semantic error,

33
00:02:02,300 --> 00:02:07,810
Python runs our code but it just doesn't end up being what we wanted it to be.

34
00:02:07,810 --> 00:02:11,810
Now, usually when we encounter a runtime error,

35
00:02:11,810 --> 00:02:16,250
then Python is going to stop running our code as soon as we encounter the runtime error.

36
00:02:16,250 --> 00:02:18,080
So, for example, this code,

37
00:02:18,080 --> 00:02:20,270
again, gives us an index error.

38
00:02:20,270 --> 00:02:26,595
So, let's suppose that I modified my code and I had something like print,

39
00:02:26,595 --> 00:02:31,150
this is not going to be reached.

40
00:02:31,760 --> 00:02:34,495
Now, if I save and run my code,

41
00:02:34,495 --> 00:02:37,850
you'll notice that this line three doesn't print

42
00:02:37,850 --> 00:02:42,590
out because this expression results in a runtime error.

43
00:02:42,590 --> 00:02:46,560
So, again, when you get a runtime error right here,

44
00:02:48,440 --> 00:02:54,520
then our program just stops running and throws in this case an index error.

45
00:02:54,520 --> 00:02:58,070
If I had a print statement before this line,

46
00:02:58,470 --> 00:03:02,410
this is going to be printed,

47
00:03:02,410 --> 00:03:05,620
then this would successfully execute.

48
00:03:05,620 --> 00:03:09,335
So, in this lesson we're going to learn about Try Except.

49
00:03:09,335 --> 00:03:10,690
What Try Except does,

50
00:03:10,690 --> 00:03:13,940
is it allows us to handle runtime errors and

51
00:03:13,940 --> 00:03:17,500
to tell Python what to do when it encounters a runtime error,

52
00:03:17,500 --> 00:03:22,520
so it doesn't suddenly stop running your program every time it encounters one.

53
00:03:23,660 --> 00:03:29,320
So, let's say that I wanted to run the code after assigning third.

54
00:03:29,320 --> 00:03:32,345
So, let's suppose that again I have a print,

55
00:03:32,345 --> 00:03:36,770
I want this to run statement and I

56
00:03:36,770 --> 00:03:42,485
want this line to print out even if I encounter a runtime error on line two.

57
00:03:42,485 --> 00:03:46,490
The way that I can do that is by putting that into a Try Except block.

58
00:03:46,490 --> 00:03:50,390
So, the simplest way to write a Try Except block is just by saying,

59
00:03:50,390 --> 00:03:57,215
try and then colon and then code inside of the try block,

60
00:03:57,215 --> 00:03:59,915
and then I can write except,

61
00:03:59,915 --> 00:04:06,470
colon, and then code that I want to run if some runtime error happens.

62
00:04:06,470 --> 00:04:11,705
So, let's suppose that if I get a runtime error while running line four,

63
00:04:11,705 --> 00:04:14,840
let's suppose that I want to assign third to be false.

64
00:04:14,840 --> 00:04:20,935
So, I'll say except third equals false.

65
00:04:20,935 --> 00:04:23,335
Now, when I run my code,

66
00:04:23,335 --> 00:04:27,660
it's not going to show us a runtime error anymore,

67
00:04:27,660 --> 00:04:29,885
but instead I actually enabled a reach

68
00:04:29,885 --> 00:04:34,070
this print statement even though I get a runtime error right here.

69
00:04:34,070 --> 00:04:35,630
So, to see what's happening,

70
00:04:35,630 --> 00:04:37,615
I'm going to add a few more print statements.

71
00:04:37,615 --> 00:04:44,240
So, inside of this except I'm going to print something went wrong,

72
00:04:44,820 --> 00:04:51,190
and I'm going to add print a on line

73
00:04:51,190 --> 00:04:57,570
four and after assigning third,

74
00:04:57,570 --> 00:05:02,840
I'm going to print out b.

75
00:05:02,840 --> 00:05:04,830
Okay. When I run my code,

76
00:05:04,830 --> 00:05:10,260
you'll notice that I have print a, print a executes.

77
00:05:10,260 --> 00:05:13,300
Then when I set third equal to items two,

78
00:05:13,300 --> 00:05:16,010
this gives us a runtime error.

79
00:05:18,710 --> 00:05:21,405
As a result of this runtime error,

80
00:05:21,405 --> 00:05:23,795
print b you'll see, never executes.

81
00:05:23,795 --> 00:05:25,020
So, what happens is,

82
00:05:25,020 --> 00:05:28,900
as soon as I get a runtime error for any line in this try-block,

83
00:05:28,900 --> 00:05:31,330
then Python jumps right to

84
00:05:31,330 --> 00:05:34,420
the except block and starts running what's in the except block.

85
00:05:34,420 --> 00:05:36,980
So, you'll see that something went wrong,

86
00:05:36,980 --> 00:05:41,440
gets executed and then here we assign third to be false,

87
00:05:41,440 --> 00:05:43,690
and then I want this to run,

88
00:05:43,690 --> 00:05:45,950
gets executed after that.

89
00:05:45,950 --> 00:05:49,915
Now, let's suppose that we didn't actually have a runtime error.

90
00:05:49,915 --> 00:05:51,370
So, I'm going to clear

91
00:05:51,370 --> 00:05:57,410
my output and I'm going to make it so that items actually does have three items.

92
00:05:58,080 --> 00:06:00,515
Now, if I run my code,

93
00:06:00,515 --> 00:06:01,850
then what happens is,

94
00:06:01,850 --> 00:06:03,940
I never get a runtime error when assigning

95
00:06:03,940 --> 00:06:06,385
third because I actually have a third item here.

96
00:06:06,385 --> 00:06:07,990
So, a gets printed,

97
00:06:07,990 --> 00:06:10,060
third gets assigned to c,

98
00:06:10,060 --> 00:06:11,920
and then b gets printed,

99
00:06:11,920 --> 00:06:16,410
and then we completely skipped the except block because nothing went wrong,

100
00:06:16,410 --> 00:06:17,775
and then we print out,

101
00:06:17,775 --> 00:06:19,695
I want this to run.

102
00:06:19,695 --> 00:06:22,705
So, in brief, what Try Except does,

103
00:06:22,705 --> 00:06:27,965
is it says try to run the code inside of this try block and if

104
00:06:27,965 --> 00:06:30,770
something goes wrong while you're running that code

105
00:06:30,770 --> 00:06:33,995
rather than stopping execution of the whole program,

106
00:06:33,995 --> 00:06:37,715
just jump and run whatever is inside of the except block.

107
00:06:37,715 --> 00:06:39,260
If nothing goes wrong,

108
00:06:39,260 --> 00:06:44,435
then we just skip the except block entirely and run what's after the Try Except.

109
00:06:44,435 --> 00:06:47,640
That's all for now, until next time.