1
00:00:01,129 --> 00:00:06,899
Hi again! Let's have one more lecture
about errors in Python and this

2
00:00:06,899 --> 00:00:11,429
particular lecture will be about
handling errors. Handling errors can be

3
00:00:11,429 --> 00:00:16,590
an advanced concept to digest for now
because you're still going through the

4
00:00:16,590 --> 00:00:22,199
basics of Python and you haven't seen
any real application yet, but later

5
00:00:22,199 --> 00:00:27,990
will use error handling while we are
building real-world applications so

6
00:00:27,990 --> 00:00:32,399
you're going to understand error
handling better later on. For now let's

7
00:00:32,399 --> 00:00:35,670
just focus on the syntax and try to
understand as much as you can

8
00:00:35,670 --> 00:00:42,899
I'll go and do an example here which I
mentioned earlier. Let's say we have a

9
00:00:42,899 --> 00:00:54,090
function that does division between two
given numbers like that. Return a divided

10
00:00:54,090 --> 00:00:59,719
by b and then you call the function here.
Divide 1, 2. In this case the

11
00:01:05,640 --> 00:01:10,619
function will try to divide 1 by 2.
And let me print out the output so that

12
00:01:10,619 --> 00:01:20,159
we can see it on the terminal. Go to the
terminal and execute the script. So you

13
00:01:20,159 --> 00:01:28,460
get you get 0.5. Now that's good.
However if the user enters let's say

14
00:01:28,460 --> 00:01:35,790
zero there like that, execute and your
program will crash so it will show an

15
00:01:35,790 --> 00:01:41,939
error. Now this one here is an example of
a longer error message that I mentioned

16
00:01:41,939 --> 00:01:45,750
previously in the previous videos that
you might have more than one, more

17
00:01:45,750 --> 00:01:54,869
than one error in your traceback, so for
instance this says line 4 at this line

18
00:01:54,869 --> 00:02:01,259
here. So that line is where Python
captured the error at first

19
00:02:01,259 --> 00:02:06,360
so basically Python was trying to
execute this line but it couldn't do it,

20
00:02:06,360 --> 00:02:10,709
it didn't manage to do it because this
line was trying to get the information

21
00:02:10,709 --> 00:02:15,460
from this function
and this function was trying to do this

22
00:02:15,460 --> 00:02:22,280
mathematical operation using zero after
the division so that is not possible and

23
00:02:22,280 --> 00:02:27,950
Python is pointing that out here.
So that's the main error in line two

24
00:02:27,950 --> 00:02:32,510
in this expression so that's where you
should focus, however when you ask

25
00:02:32,510 --> 00:02:36,590
questions you should include the entire
error message in your question

26
00:02:36,590 --> 00:02:43,550
as I mentioned previously.
So back to handling errors now. What I'm

27
00:02:43,550 --> 00:02:49,850
trying to say here is that you know
you have other functions here and other

28
00:02:49,850 --> 00:02:57,320
lines of code that you want to execute,
that you want Python to execute, but if

29
00:02:57,320 --> 00:03:04,720
the user passed this zero number here
the other lines would not be executed

30
00:03:04,720 --> 00:03:09,680
so your program would crash and you
don't want that and the way to avoid

31
00:03:09,680 --> 00:03:17,720
that is by using try and accept so
basically you try to capture the

32
00:03:17,720 --> 00:03:24,170
exception, to handle the exception.
The way to do that is quite easy.

33
00:03:24,170 --> 00:03:33,860
You say try and return a/b, so this has to be
indented like that so try is indented

34
00:03:33,860 --> 00:03:40,160
because it's under the function
definition. And then return is indented

35
00:03:40,160 --> 00:03:49,190
because it's under the try expression.
Then you have except, execute so this

36
00:03:49,190 --> 00:03:54,470
has to be on the same indentation level
with try, they are the same block, so

37
00:03:54,470 --> 00:04:02,090
except if there is an exception. What you
want to do is print out: "You are

38
00:04:02,090 --> 00:04:07,540
dividing by zero", or
"�is this meaningless". So let me try

39
00:04:16,299 --> 00:04:26,380
this now and you get: "Zero division is
meaningless" and none, oh sorry I meant

40
00:04:26,380 --> 00:04:34,420
to do here return, not print.
I explained you why you get none there so you

41
00:04:34,420 --> 00:04:42,940
got none as an output because what
the function did was its tried to do

42
00:04:42,940 --> 00:04:48,160
this expression but it got an error
so it ignores this line, it doesn't

43
00:04:48,160 --> 00:04:54,880
execute it and then it goes to the
next line. So this line is executed when

44
00:04:54,880 --> 00:05:01,540
this is not executed and so it printed
out: "Zero division is meaningless", but it

45
00:05:01,540 --> 00:05:06,010
also printed out none because when you
execute a function that doesn't have a

46
00:05:06,010 --> 00:05:10,450
return statement such as in this case we
had the print statement here, the print

47
00:05:10,450 --> 00:05:15,820
function not the return statement, in that
case if the function is not returning

48
00:05:15,820 --> 00:05:23,440
anything it can have a print function or
other function as well, but if it

49
00:05:23,440 --> 00:05:29,229
doesn't have a return statement
then it will print out none, so basically

50
00:05:29,229 --> 00:05:33,520
it executes the other procedures such as
the print statement, it prints out the

51
00:05:33,520 --> 00:05:37,690
statement but if it doesn't have
a return statement it doesn't, it prints

52
00:05:37,690 --> 00:05:43,030
out none. Anyway save this now, execute
again and in this case you get only this

53
00:05:43,030 --> 00:05:48,729
zero division is meaningless printed
out there. So basically if you had other

54
00:05:48,729 --> 00:05:57,280
lines of code here like that, those would
be executed as well and that happens by

55
00:05:57,280 --> 00:06:03,160
using the try and except block who which
make sure that the code flow continues.

56
00:06:03,160 --> 00:06:11,050
So while the important thing to note is
that it is advisable to actually here use

57
00:06:11,050 --> 00:06:18,120
the exact name of the error that you
want to accept, so

58
00:06:19,350 --> 00:06:27,340
Zero Division Error and as you see these
got turned into blue which means that

59
00:06:27,340 --> 00:06:33,370
this is actually a Python keyword, is
recognized by Python and in that case if

60
00:06:33,370 --> 00:06:41,560
you exact you this you get the same
output actually like that or if you pass

61
00:06:41,560 --> 00:06:46,650
a number there that it is possible to
get a division from, you'll get that.

62
00:06:46,650 --> 00:06:53,020
Sorry about the backslash. I typed that
by mistake on my terminal. Execute

63
00:06:53,020 --> 00:06:56,650
again so you get this. And so what I'm
trying to say about the zero division

64
00:06:56,650 --> 00:07:01,090
error is that you know sometimes you
might get other types of errors in your

65
00:07:01,090 --> 00:07:07,930
code, other types of exceptions.
Let's say you have a name error there

66
00:07:07,930 --> 00:07:12,370
and you are saying that except all
errors when you don't pass this you

67
00:07:12,370 --> 00:07:17,919
you're basically including all types of
errors there and that is a bit dangerous for your

68
00:07:17,919 --> 00:07:22,960
code because you know you might have
problems that you don't notice and

69
00:07:22,960 --> 00:07:27,789
basically you are ignoring that, but you
shouldn't. So you should be specific

70
00:07:27,789 --> 00:07:32,830
there, explicit like that. If there are
other types of errors such as a name error,

71
00:07:32,830 --> 00:07:38,349
that name error will crash your
program, so you know what's going on.

72
00:07:38,349 --> 00:07:42,760
You need to be specific.
If you want to handle that error too, then

73
00:07:42,760 --> 00:07:47,380
you can add another exception here.
Except name error for instance. Yeah,

74
00:07:47,380 --> 00:07:52,389
that's about handling errors in Python.
I hope this makes sense! I hope I explained

75
00:07:52,389 --> 00:07:58,330
this well! And yeah, this is about errors.
I'll talk to you in the next lectures.

76
00:07:58,330 --> 00:08:00,000
See you!

