0:00
hello everyone my name is Ja and today
0:02
I'm going to teach you how to use TR
0:04
except else finally in this Python
0:07
tutorial so the try and except statement
0:09
at python is a control flow statement
0:12
that is used for you to handle the
0:14
errors as they happen in your code so
0:16
the idea is to you that you run a block
0:18
of code and if there's an error you run
0:20
in something else uh and this prevents
0:24
your code from breaking because whenever
0:26
you have like exceptions like this hat
0:29
run time you can say that okay one
0:32
divided by 0 that you are not allowed to
0:34
do this but you want to print one
0:37
divided by 0 then you cannot do a
0:39
division by zero so the code will return
0:42
that error everything will break and
0:45
your code won't be able to process any
0:48
line that comes after that exception so
0:51
in order to handle this we need to use
0:53
the try an accept statement and return
0:56
code within it so we can copy that
0:58
example that we had and do try so that's
1:02
the structure you do try and then you
1:07
accept and you trade there was an
1:13
error so we say okay if this breaks then
1:18
return that error and in this case you
1:20
can see that the code managed to to work
1:22
so if you were to reprint hello here uh
1:26
it would still execute properly because
1:30
you wrapped in your error whereas if I
1:34
were to come back here and I wanted to
1:36
print hello at the end it would never
1:38
have printed it so this is the the very
1:41
very basic and powerful thing that uh
1:44
try and except Sofer is it allows you to
1:48
prevent whatever uh to predict whatever
1:50
is coming up and to fix your code so
1:53
that your code doesn't break when uh
1:55
something happens so if we can also
1:58
return uh the name of the exception
2:03
here here as exception name so we could
2:08
say okay there was an error and we could
2:11
just return that exception name right
2:13
there there was an an error division by
2:17
zero if you don't know that this is a
2:19
division uh by zero what you could do is
2:22
just say exception so you just say
2:26
whatever exception you get then return
2:28
the name of the ex exeption so if it's
2:31
not a division by zero then in this case
2:34
it wouldn't uh break so let's say you
2:37
want to do int hello in this case it
2:41
would return inv invalid literal base
2:48
that's pretty much everything you need
2:50
to know for most of the use case that
2:53
you'll get as you go further into uh the
2:58
the logic here is you can have uh this
3:02
example and maybe you want to do the
3:05
exception and if it works then so you
3:09
try and accept and if it works just
3:12
print uh something else
3:15
so there was no error and
3:20
X so you could say okay if you have this
3:25
uh there is a division error because you
3:28
cannot do this but let's say you want to
3:30
do a real division that works then you
3:32
can do 10 ided by two and it will see
3:36
that if it manages to uh run the code
3:39
then else it also uh will return
3:42
something that you wanted so it returns
3:50
error next you can have the finally
3:53
statement something that is not used as
3:56
much as the others uh but the final St
4:00
statement allows you to run code
4:02
regardless of if there was an exception
4:05
or not so one thing you can have is
4:08
let's say we have try and accept and we
4:12
division but we can use the finally
4:16
keyword and if there is an error or if
4:20
there's no error you write something
4:23
that uh try and except has finished
4:33
executing so you can run this so
4:36
regardless of if there is an error if
4:38
you do 10 divided by two you wrote like
4:42
you can divide by zero but if we run
4:44
something uh then it will return try and
4:47
accept it will always return try and
4:52
execution you can also use uh the raise
4:56
statement the raise statement is the
4:58
keyword that is used to raise your own
5:00
exception in your code so if you build
5:03
like code that you know that people are
5:04
going to use sometimes you want to
5:07
actually uh uh raise an exception so
5:10
let's say this best TV show and my best
5:13
TV show ever is Dawson everybody loves
5:16
Dawson I don't want to wait you know
5:19
that TV show I don't know uh it's really
5:22
crappy Tv show uh but the best TV show
5:27
and if best TV show is not equal to How
5:31
I Met Your Mother which is actually the
5:34
best TB show ever uh then you raise an
5:37
exception so I my code is very important
5:40
here uh if people write something that
5:44
is not exactly what I want uh I just
5:47
raise an exception and I can throw in uh
5:50
whatever exception I want like if you
5:52
just put a best TV show equal dson then
5:56
you raise an exception this is a dumb
5:59
answer and in this case I have raised my
6:01
own exception in my code uh and this is
6:04
essentially what the libraries have done
6:07
uh for you to know what's going on in
6:10
the code so thank you very much this is
6:12
over for try accept and finally uh And
6:16
subscribe to my Channel or follow on my
6:19
website you'll find a lot of resources
6:21
for python for beginners thank you very
6:24
much see you next time